-2

I have an input string/text like this:

    <span id="salutation">


            Mister




</span><div class="c"></div>

With which pattern can I get the string Mister?

This pattern:

string pattern = "<span id=\"salutation\"> (.*) </span>";

have no success for me.

mickmackusa
  • 37,596
  • 11
  • 75
  • 105
Brandon Baba
  • 13
  • 1
  • 3

1 Answers1

0

The correct regex is:

<span id="salutation">\s*(.*?)\s*</span>

which leaves out all the spaces (\s) around "Mister".

But, as in some other language, you have to double escape because of the slash:

"<span id=\"salutation\">\\s*(.*?)\\s*</span>"

In C# you can even use this trick:

@"<span id=""salutation"">\s*(.*?)\s*</span>"

Anyway, regex are not the best tool for this. Try to use an HTML parser (see What is the best way to parse html in C#? [closed]).

horcrux
  • 6,493
  • 6
  • 27
  • 40