3
Reply

Getting the first instance of the matching string from a string using regular expression (confusing?!)

Ask a question
NLV Raghavendra

NLV Raghavendra

14 years ago
5.8k
1
Hello,

Let me explain. I have a string bigstring = 1234abcd4. Now i want to get all the substrings starting with 1 and ending with 4 in the bigstring.

So i wrote this code.


Regex reg = new Regex("1(.*)4");
Match matches = reg.Match(bigstring);
string matchstr = matches.Groups[0].Value;

But i'm always getting 1234abcd4 and i want 1234 which is the first instance that matches. How can we do that?

Any ideas?

Thank you.

Answers (3)