tags:

views:

33

answers:

1

I've been trying desperately to return a text string in addition to captures groups from a regular expression using alternation. Here's what I have;

(?<make>(?:\w*|\w*\s\w*));(?<score1>\d{1,2});(?<score2>\d{1,2});(?<model>\w{1,})(?(model)No|Yes)

My Data;

Austin;1;2;Taxi
Audi;2;4;Quattro
BMW;4;5;M3
Ferrari;10;10;F40
Fiat;4;2;Panda

All the capture groups work perfectly, however when I add the alternation statement (?(model)No|Yes) it fails, and nothing is returned. I'm a bit stuck and have tried rearranging the expression in many ways, the application Expresso says the last part of the statement is a conditional expression with a yes and no clause, "did the capture named [model] match?"

Any help would be greatly appreciated!

+2  A: 

The alternation statement means: If the named group model has matched, then try to match No, if it hasn't, then try to match Yes. Both fail for obvious reasons. What do you intend to do with the alternation?

Tim Pietzcker
Mmmmm, think I got confused because I've been able to return no as an output. I understand what your saying now, after a think.What I was trying todo was substitute the make with another, based on whether I found it or not. Returning a slightly different string, for example Quattro would be returned as A3.
wonea
I see, but as Max S. wrote, that can't be done in a regex, only in your program.
Tim Pietzcker