Regex works in python, but not in C#

  • Thread starter Thread starter Fencer
  • Start date Start date
F

Fencer

Hello, say I have this string

abcde123:dsdls:dlsds

and I wanted to use regular expressions to extract abcde123.

In python I could do:
str = 'abcde123:dsdls:dlsds'
import re
matches = re.match('^([^:]*)', str)
print matches.group(1)
abcde123

In C#, however, when I use:
Regex.Match("^([^:]*)", "abcde123:dsdls:dlsds")

The Match object m returned has the property Success set to False and
there's just an empty string in m.Groups[1].

I know there other (better) ways of extracting the particular substring
in this example, but I'm still wondering why it didn't work in C# and,
based on the Python code, how I should rewrite the C# version?

I've just returned to C# after being away for a long time so I'm very
rusty indeed (and I didn't know it all that well before, either).

Thanks!

- Fencer
 
Fencer said:
In C#, however, when I use:
Regex.Match("^([^:]*)", "abcde123:dsdls:dlsds")

The Match object m returned has the property Success set to False and
there's just an empty string in m.Groups[1].

You have the strings for the input and pattern reversed.
 
Back
Top