I have a string I need to split using a Regex. I could use the split methods in .NET but the string to return could be the first or second substrings. This regex will become a configurable setting in an application.
A typical string would be 9234567X123456-789
I’ve create the following regex
[-09]([^X]*)X
to return the first substring. However, I lose the first digit, 234567 is returned.
Any ideas?
Thanks.
I’m assuming that you want the string to start with either
-,0or9. If this is true the regex you need (and using regexes here is overkill unless you want to also perform a sort of validation) isOR
if you are allowing any kind of character (not only digits) in the part before the
XEDIT:
Might have understood what you are looking for reading your sample string:
You need either the first or the second group and every group can have digits or hypen (
-). You solve this by creating two groups:then you can check Jon Skeet’s answer to get the data. You could either use
or