Re: Question relating regexp
Robert Land wrote:
> An example from the phone list:
> ....
> 1248 Kate 634
> ....
> 1548 Kerry 534
> ....
>
> To match a line that starts with a 1,
> has some digits, at least one space
> and a name that starts with a K we can write:
>
> grep '^1[0-9]\{1,\} \{1,\}K' phonelist.txt
> or use * and repeat [0-9] and space:
> grep '^1[0-9][0-9]* *K' phonelist.txt
> "
>
> =Why, in the first example, has the author
> prefaced the char 'K' with the one or more
> times multiplier? He only wants to find a
> name beginning with 'K'(!)
Actually, the one or more times multiplier is applied to
the space that it follows, not to the K that it precedes.
> Then, in the snd grep command he doubled
> '[0-9]', wouldn't only '^1[0-9]*....'be
> sufficent? Again, 'K' is prefaced with the
> asterisk which doesn't seem necessary to me.
the example means this:
A line starting with a 1, then a digit, then zero or more
digits, then a space, then zero or more spaces, then a K.
Again, the star refers to the space that it follows, not to
the K that it precedes.
So as far as I can see, the examples give the same result.
> I hope one day all would agree on one standard.
>
> Robert
I personally prefer the second version, but I guess it's a
matter of taste :-)
--
Best regards,
Peter Hugosson-Miller
"There are 10 kinds of people. Those who can count in
binary and those who can't."
Reply to: