Operating System - HP-UX
1846585 Members
3108 Online
110256 Solutions
New Discussion

Re: Regular expression result

 
SOLVED
Go to solution
Sébastien N
Advisor

Regular expression result

Hi,

I write :
echo "TRU 123ZER" | sed 's/.*\([0-9][0-9]*\).*/\1/'
and the result is "3".

Shouldn't it be "123" ?*

Thanx.
4 REPLIES 4
Umapathy S
Honored Contributor
Solution

Re: Regular expression result

Sebastian,
I think there should a space between first .*

echo "TRU 123ZER" | sed 's/.* \([0-9][0-9]*\).*/\1/'

HTH,
Umapathy

Arise Awake and Stop NOT till the goal is Reached!
Brian Bergstrand
Honored Contributor

Re: Regular expression result

You are running into RE's "match the most I can" policy. You need to break qualify your requirements more.

This should work better:

echo "TRU 123ZER" | sed 's/[^0-9]*\([0-9][0-9]*\).*/\1/'

HTH.
Sébastien N
Advisor

Re: Regular expression result

Both are working.
But I don't understand why...


Thanx a lot.

what does HTH mean ?
Umapathy S
Honored Contributor

Re: Regular expression result

Sebastian,
As Brian said, "match the most I can" is done by .* which expands to a char followed by any number of chars that include a space also.
The backslashed number represents the group of chars within the braces. Here in your exp you match only the numbers with your braces and thats get printed.
Without spaces all the numbers except 3 is gathered by the first .*.

HTH means Hope This Helps.

HTH,
Umapathy

Arise Awake and Stop NOT till the goal is Reached!