You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@spamassassin.apache.org by Marcio Humpris <ma...@gmail.com> on 2013/11/01 01:53:37 UTC

trying to understand capture groups/repetition (regex)

Can someone kindly explain a regex?

Lose .{0,2}.{0,1}(KG|Quilos|Kilos|Pounds|Lb)

It matches fine lose 22 pounds, lose 5 pounds. Now 3 digits such as lose
100 pounds it doesnt which is ok.

I was trying to understand those capture groups .{0,2}.{0,1}

. is any character and the 0,2 seems to be repetitions, but i really dont
understand it.

Thanks

Re: trying to understand capture groups/repetition (regex)

Posted by John Hardin <jh...@impsec.org>.
On Thu, 31 Oct 2013, Marcio Humpris wrote:

> Can someone kindly explain a regex?
>
> Lose .{0,2}.{0,1}(KG|Quilos|Kilos|Pounds|Lb)
>
> It matches fine lose 22 pounds, lose 5 pounds. Now 3 digits such as lose
> 100 pounds it doesnt which is ok.
>
> I was trying to understand those capture groups .{0,2}.{0,1}
>
> . is any character and the 0,2 seems to be repetitions, but i really dont
> understand it.

{0,2} means "from zero to 2 repetitions".

Three digits should match that RE because it matches up to three of 
anything before the explicit text. However, if it was three digits *plus a 
space* it would not match because the space has to be matched too - 
that's *four* characters there.

That's an ugly and inefficient RE. If you're trying to match an optional 
number there, here's a much better way to do it:

 	/Lose (?:\d+\s)?(?:KG|Quilos|Kilos|Pounds|Lb)/

Breakdown:
 	"Lose ", followed by
 	(one or more digits followed by a space) maybe?, followed by
 	(one of the specified strings)


-- 
  John Hardin KA7OHZ                    http://www.impsec.org/~jhardin/
  jhardin@impsec.org    FALaholic #11174     pgpk -a jhardin@impsec.org
  key: 0xB8732E79 -- 2D8C 34F4 6411 F507 136C  AF76 D822 E6E6 B873 2E79
-----------------------------------------------------------------------
   ...the Fates notice those who buy chainsaws...
                                               -- www.darwinawards.com
-----------------------------------------------------------------------
  Today: Halloween