4

How do I make Regex stop the search after "Target This"?

HeaderText="Target This" AnotherAttribute="Getting Picked Up"

This is what i've tried

var match = Regex.Match(string1, @"(?<=HeaderText=\").*(?=\")");
Rod
  • 14,529
  • 31
  • 118
  • 230

3 Answers3

2

The quantifier * is eager, which means it will consume as many characters as it can while still getting a match. You want the lazy quantifier, *?.

As an aside, rather than using look-around expressions as you have done here, you may find it in general easier to use capturing groups:

var match = Regex.Match(string1, "HeaderText=\"(.*?)\"");
                                               ^   ^ these make a capturing group

Now the match matches the whole thing, but match.Groups[1] is just the value in the quotes.

AakashM
  • 62,551
  • 17
  • 151
  • 186
1

Plain regex pattern

(?<=HeaderText=").*?(?=")

or as string

string pattern = "(?<=HeaderText=\").*?(?=\")";

or using a verbatim string

string pattern = @"(?<=HeaderText="").*?(?="")";

The trick is the question mark after .*. It means "as few as possible", making it stop after the first end-quotes it encounters.

Note that verbatim strings (introduced with @) do not recognize the backslash \ as escape character. Escape the double quotes by doubling them.

Note for others interested in regex: The search pattern used finds a postion between a prefix and a suffix:

(?<=prefix)find(?=suffix)
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
1

Try this:

var match = Regex.Match(string1, "HeaderText=\"([^\"]+)");
var val = match.Groups[1].Value; //Target This

UPDATE if there possibilities have double quotes in target,change the regex to:

HeaderText=\"(.+?)\"\\s+\\w

Note: it's not right way to do this, if it's a XML, check out System.XML otherwise,HtmlAgilityPack / How to use HTML Agility pack.

Community
  • 1
  • 1
The Mask
  • 17,007
  • 37
  • 111
  • 185