tags:

views:

377

answers:

3

I am working with legacy systems at the moment, and a lot of work involves breaking up delimited strings and testing against certain rules.

With this string, how could I return "Active" in a back reference and search terms, stopping when it hits the first caret (^)?:

Active^20080505^900^LT^100

Can it be done with an inclusion in the regex of this "(.+)" ? The reason I ask is that the actual regex "(.+)" is defined in a database as cutting up these messages and their associated rules can be set from a front-end system. The content could be anything ('Active' in this case), that's why ".+" has been used in this case.

Rule: The caret sign cannot feature between the brackets, as that would result with it being stored in the database field too, and it is defined elsewhere in another system field.

If you have a better suggestion than "(.+)" will be happy to hear it.

Thanks in advance.

+3  A: 
(.+?)\^

Should grab up to the first ^

If you have to include (.+) w/o modifications you could use this:

(.+?)\^(.+)

The first backreference will still be the correct one and you can ignore the second.

Gordon Wilson
Thanks Gordon, works perfectly! Astonishing. Now as recursive said, I need to make a decision on droppping consistency in favour of efficiency.
GONeale
A: 
^([^\^]+)

That should work if your RE library doesn't support non-greediness.

dreamlax
Yes I'm aware of that approach, but realised I have to honour the ".+".
GONeale
+1  A: 

A regex is really overkill here.

Just take the first n characters of the string where n is the position of the first caret.

Pseudo code:

InputString.Left(InputString.IndexOf("^"))
recursive
I know. Because this is an 'engine' everything used regexes, and for consistency I didn't think otherwise, but on this particular, simpler message type I now need to support, perhaps grabbing the left of the string to the ^ would be the way to go. Thanks.
GONeale