Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex syntax stop search

Tags:

c#

regex

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=\").*(?=\")");
like image 927
Rod Avatar asked Nov 03 '25 11:11

Rod


1 Answers

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.

like image 51
AakashM Avatar answered Nov 05 '25 01:11

AakashM