Re: Question relating regexp
On Fri, Jan 17, 2003 at 03:03:33PM +0100, 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'(!)
>
> 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.
In both cases, you're assuming that modifiers like \{...\} and * modify
the atom following them. This is not true; they modify the atom
*preceding* them. So / \{1,\}K/ and / *K/ both mean "at least one space
followed by a K".
With regard to /1[0-9][0-9]*/ versus /1[0-9]*/, it appears that the
author requires there to be at least two digits in any number. /1[0-9]*/
would match, among other things, the simple "1".
Cheers,
--
Colin Watson [cjwatson@flatline.org.uk]
Reply to: