John P. John P. - 15 days ago 12
C# Question

Regex get string between two strings

I am using

HTMLAgilityPack
to get some classes and then from the
innerText
of the classes I want to get a collection of a
Regex
matches.

My string looks like this:

somestring<b>Test:<br>
EUR/USD - &quot;Expect for <i>PRICE BREAKOUT</i>&quot;<br>
Sell : 1.06809<br>
Target : 1.06309<br>
Stop Loss : 1.07309</b><br>rest of html


and my Regex looks like this

MatchCollection matches = Regex.Matches(sString, @"<b>(.*?)</b>");


However there is not a match. Normally in the text there should be 5 results(the string above repeats itself with slightly changes).
There must be something with the special characters that I am missing but I cant seem to figure it out.

Please lend me a hand here. I know its a noobie question but its a bit tricky for me.

Thank you in advance

Answer

You need to specify the SingleLine flag to ignore newline characters and escape the forward slash.

MatchCollection matches = Regex.Matches(sString, @"<b>(.*)<\/b>", RegexOptions.Singleline);

See: https://regex101.com/r/zK1vG9/1