[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Question relating regexp



On Fri, Jan 17, 2003 at 03:03:33PM +0100, Robert Land wrote:
> 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'(!)
Sure, but the multipliers don't affect the following expression;
the work for the preceding one. That means, the first expression
says "1, one or more numbers, one or more spaces, 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.
No, it wouldn't. You want the "1" at the beginning to be followed
by _at least_ one more number. The "*" means "zero or more",
so [0-9]* matches either some digits or none. It translates to
"there might as well be digits, but not necessarily". So the
second expression means "1, any number, possible number(s), space,
more possible space, K".

Thus:	[0-9] = a number;
		[0-9]* = either no number, exactly one, or some numbers;
		[0-9]\{2,4\} = between two and four numbers;
		[0-9]\{0,\} = zero or more; equals [0-9]*

	andrej
-- 
echo ${girl_name} > /etc/dumpdates



Reply to: