[H-GEN] REGEX pattern required

Byron Ellacott bje at apnic.net
Tue Dec 7 22:42:55 EST 2004


Andrae Muys wrote:
> Gary Curtis wrote:
>> Can this be accomplished with just one pattern?
> This becomes ([^0-9]*)([0-9]*)(.*)  escape and substitute appropriate 
> character class labels to suit.


As discussed on #humbug, this doesn't quite work; it will match the 
left-most number group, not the right-most.

The exact syntax to use will depend on the flavour of regex you're 
using.  The following in perl will do what you want:

   /^(.*?)([0-9]+)([^0-9]*)$/

The three groups are (.*?), ([0-9]+) and ([^0-9]*).  The first matches 
as few characters as necessary to pad the expression out -- the ? 
modifier makes the * modifier non-greedy.  The second matches one or 
more digits.  The third expression requires that everything after the 
digit sequence be non-digits, that is, the digit sequence must match the 
right-most digit sequence.

If you're using a different regex engine, you will need this adjusted to 
suit.

-- 
bje




More information about the General mailing list