Mel
Mel

Reputation: 3128

.NET Regex using look behind/ahead

why does this regex fail to match:

(?<="Title" = "8:)[^"]*(?=")

this:

"ARPHELPTELEPHONE" = "8:"
"ARPHELPLINK" = "8:"
"Title" = "8:something"
"Subject" = "8:"
"ARPCONTACT" = "8:something"
"Keywords" = "8:"

Its in excerpt from a visual studio setup project. It matches in the online tool i'm using but it doesn't match using Regex.Match()

Here's the code:

var productTitleRegex = @"(?<=""Title"" = ""8:)[^""]*(?="")";
var titleMatch = Regex.Match(content, productTitleRegex);

titleMatch.Success if false and value returns nothing

EDIT:

Maybe I'm just missing something when using Look ahead and look behind in .net regex? Because if I remove the lookarounds, this works but it matches the whole line, which I don't want. So I used lookarounds to just match the value I want.

Anyone else had a similar experience when using lookarounds in .net regex?

Upvotes: 0

Views: 998

Answers (1)

Riju
Riju

Reputation: 576

I tested your regex pattern with C# in VS2008, it found a match with "something" as value. looks like it is working fine. I also tested it with free tool downloaded from http://www.radsoftware.com.au/regexdesigner/

enter image description here

However It won't work if your using RegEX with RegexOptions "IgnorePatternWhitespace"

    var productTitleRegex = @"(?<=""Title"" = ""8:)[^""]*(?="")";
    var titleMatch = Regex.Match(content, productTitleRegex, RegexOptions.IgnorePatternWhitespace);

in this case you won't get any match

Upvotes: 1

Related Questions