-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
  • 43,625
  • 12
  • 83
  • 136
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]).

logi-kal
  • 7,107
  • 6
  • 31
  • 43