0

I would like to ask, how to define Regex in C#

i have a text box, where user will write name of author, who should be find. i want to define Regex which will find in HTML content

<span class ="author">John</span>
<span class ="song">Hooray</span>

something like: user put John into textbox, program retrieves him Hooray

My attempt:

MatchCollection m1 = Regex.Matches(html, @"<span class=("[\w\d]*")\sauthor=("[\w\d]*")>([\w\d]*)</span>", RegexOptions.Singleline);
DRastislav
  • 1,892
  • 3
  • 26
  • 40

1 Answers1

2

As stated in the comments, nothing good ever comes from using regex on HTML. However, if - and only if - the markup is as simple as in your example, you could probably get away with something like this:

    @"<span class=""author"">(?<author>[^<]+)</span>" 

The authors name could be retrieved from match.Groups["author"].Value

Henrik Nielsen
  • 410
  • 2
  • 6