Regex works in python, but not in C#

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
 
S

Scott Seligman

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top