PDA

View Full Version : RegExp question



high_flyer
26th August 2007, 17:24
Hi,

I want to match the following cases:
somename-123
somename_123
Where 'somename' should be any string of letters (any case) then a '-' OR a '_' and then optionally a number.
Here is my RegExp:
[a-zA-Z]+_|\-[a-zA-Z]+[0-9]

But it only matches 'somename_'. not 'somename-'
If switch between '_' and '-' so:
[a-zA-Z]+\-|_[a-zA-Z]+[0-9]
then only 'somename-' will match - so only the first between then two (\-|_) will match.
Why?

I tried also
[a-zA-Z]+(_|\-)[a-zA-Z]+[0-9] which matches nothing then.

Thanks in advance.

jacek
26th August 2007, 18:23
[a-zA-Z]+_|\-[a-zA-Z]+[0-9]
This means: match (one or more letters followed by "_") or ("-" followed by one or more letters and a digit).

What you need is: [a-zA-Z]+((_|-)[0-9]+)?
I.e. match one or more letters optionally followed by (("_" or "-" ) and one or more digits).

A bit more efficient version looks like this: [a-zA-Z]+(?:(?:_|-)[0-9]+)? (since you don't want to capture anything). You can write it also this way: [a-zA-Z]+([-_][0-9]+)? ("-" must be first).

If you have any doubts, try kregexpeditor. It displays regexps in a semigraphical form, so you can see what they mean exactly.