-1

Please check the code bellow. I want to get value of CDN$&nbsp;23.99 from following string. Please don't advice me to use htmlagilitypack because some other reason i cant use it here. I need to match the pattern of first part like <mama id="priceblock_ourprice" class="a-size-medium a-color-price priceBlockBuyingPriceString"> and get value after this text pattern until its find next pattern which is- </mama>

How can i do this?

string foo = "<mama id="priceblock_ourprice" class="a-size-medium a-color-price priceBlockBuyingPriceString">CDN$&nbsp;23.99</mama>";


string doo = "CDN$&nbsp;23.99"; //output need like this
Mr John
  • 119
  • 7
  • Surely you don't have only string foo value as shown here but a lot of arbitrary html before and after that, right? – Filburt Feb 22 '20 at 16:05
  • @Filburt yes lots of others html too but this line is unique – Mr John Feb 22 '20 at 16:07
  • I just need match that first part text pattern and take value until – Mr John Feb 22 '20 at 16:12
  • 1
    There are some answers to [RegEx match open tags except XHTML self-contained tags](https://stackoverflow.com/q/1732348/205233) and related questions that may have what you need. – Filburt Feb 22 '20 at 17:01
  • Does this answer your question? [RegEx match open tags except XHTML self-contained tags](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – Ian Kemp Feb 22 '20 at 21:07

1 Answers1

1

All you need is a Regex

string foo = "<mama id=\"priceblock_ourprice\" class=\"a-size-medium a-color-price priceBlockBuyingPriceString\">CDN$&nbsp;23.99</mama>";

Regex rx = new Regex(@"<mama .*>(.*)<\/mama>",
RegexOptions.Compiled | RegexOptions.IgnoreCase);

MatchCollection matches = rx.Matches(foo);

string doo = matches[0].Groups[1].Value;

here you have a fiddle https://dotnetfiddle.net/TBIUBA

edit: You need to access Groups[1] because under Group[0] the value that is stored is >(.*)<\/mama> so the parentesis () create groups inside the regular expressions.

If you need more "checking" for the expression at the begginging of it. Just addit on the Regexinstantiation >

TiGreX
  • 1,602
  • 3
  • 26
  • 41
  • You will need to make that a non-greedy match: "(.*?)" - try to add extra html at the start of foo – Hans Kesting Feb 22 '20 at 20:52
  • @HansKesting you're right, I was assuming that was all the possible `HTML`, just updated to check the `mama` tag – TiGreX Feb 22 '20 at 21:06