Friday, 9 August 2013

Regex match words that end in any # of digits OR "X"

Regex match words that end in any # of digits OR "X"

Trying to match all of these:
{_someWord1} ... $1=someWord, $2=1
{_another82} ... $1=another, $2=82 (item in question)
{_testX} ... $1=test, $2=X
My regex: {_(\w+)(\d+|X)} matches all three, but the groups for the 2nd
item are:
{_another82} ... $1=another8, $2=2
I'd like to be able to have any number of digits be in $2, and keep just
the words in $1. Do I need to have a look ahead of some sort? Thanks.

No comments:

Post a Comment