AaronM
AaronM

Reputation: 292

LookAhead Regex in .Net - unexpected result

I am a bit puzzled with my Regex results (and still trying to get my head around the syntax). I have been using http://regexpal.com/ to test out my expression, and its works as intended there, however in C# its not as expected.

Here is a test - an expression of the following: (?=<open>).*?(?=</open>)

on an input string of: <open>Text 1 </open>Text 2 <open>Text 3 </open>Text 4 <open>Text 5 </open>

I would expect a result back of <open>Text1 <open>Text 2 <open>Text 3... etc

However when I do this in C# it only returns the first match of <open>Text1

How do I get all five 'results' back from the Regex?

    Regex exx = new Regex("(?=<open>).*?(?=</open>)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
    string input = "<open>Text 1</open> Text 2 <open> Text 3 </open> Text 4 <open> Text 5 </open>";
    string result = Regex.Match(input, exx.ToString(), exx.Options).ToString(); 

Upvotes: 1

Views: 612

Answers (2)

Tim Pietzcker
Tim Pietzcker

Reputation: 336158

Do you really want to have <open> at the start of every match? Why not use lookbehind, too?

(?<=<open>).*?(?=</open>)

Upvotes: 0

Joey
Joey

Reputation: 354526

Use Regex.Matches instead of Regex.Match.

PS Home:> $s = '<open>Text 1 </open>Text 2 <open>Text 3 </open>Text 4 <open>Text 5 </open>'
PS Home:> $re = '(?=<open>).*?(?=</open>)'
PS Home:> @([regex]::Match($s, $re)).Length
1
PS Home:> @([regex]::Matches($s, $re)).Length
3

As the documentation for Regex.Match states:

Searches an input string for a substring that matches a regular expression pattern and returns the first occurrence as a single Match object.

whereas for Regex.Matches:

Searches an input string for all occurrences of a regular expression and returns all the successful matches.

Note: What you're doing here seems very wrong. If what you're dealing with is XML or a similar language, then please don't use regular expressions to parse it. You'll get mad otherwise with nested structures.

Upvotes: 1

Related Questions