LookAhead Regex in .Net - unexpected result

Posted by AaronM on Stack Overflow See other posts from Stack Overflow or by AaronM
Published on 2010-03-17T10:41:04Z Indexed on 2010/03/17 10:41 UTC
Read the original article Hit count: 544

Filed under:
|
|

Hello, 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(); 

© Stack Overflow or respective owner

Related posts about regex

Related posts about c#