-1

Can someone assist me to extract the price from the below string using a RegEx c#. I've tried few examples and seems I can not get it done and I know it is very basic but I can't get it.

I could not find a way to escape : and ".

String: "ouioieu":"Canister","price":"59.0000","sku":"DECC500","barcode_gtin sjh

Expected value: 59.0000

I need the complete code block as once working, I will use the same method for other places.

(Why marked as duplicate due to similar answer with double quotes?. But this one, the main issue was colon and I needed a complete answer and already received. )

BenW
  • 1,393
  • 1
  • 16
  • 26

1 Answers1

8

As a disclaimer, your data looks a lot like a fragment from JSON. If so, you should consider using a JSON parser to extract things from it. Assuming you absolutely need to use a regex here, then consider the pattern:

.*"price":"(.*?)"

This will capture everything which followed price in quotes. Here is a sample code:

string str = @"""ouioieu"":""Canister"",""price"":""59.0000"",""sku"":""DECC500"",""barcode_gtin sjh""";
var m = Regex.Match(str,@".*""price"":""(.*?)"".*");
Console.WriteLine(m.Groups[1].Value);

Output:

59.0000

Demo here:

Rextester

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360