Home > OS >  What would be the correct regex to find the value I (or other value) in "Status":"I&q
What would be the correct regex to find the value I (or other value) in "Status":"I&q

Time:12-09

I have got a long string of info and within it there would be "Status":"I",

I want to get the value of "Status", I don't need the word Status so I am trying to tell my Regex to look after "Status": which would/should be "I" ..

I'd also like to just get the value I without " but I am struggling to figure out how to actually quote the quotation mark after : ...

"(""Status"" "":"")[a-zA-Z]"
"(?<=""Status"":"")^[a-zA-Z]"
"(?<=""Status"":)^[a-zA-Z]"
"(?<=""Status"":""""")^[a-zA-Z]"

I've tried a few variations.. But I am struggling..

I'm also using " twice before and after Status as I'm using .net framework and it doesn't like me..

I'd be happy with either result being I or "I" but would prefer just I , Any suggestions?

Example

string invoiceId = Regex.Match(invoiceResult, @"(?<=""IssuedInvoiceId"":)\d{7}").ToString();

string status = Regex.Match(invoiceResult, @"(?<=""Status"":""""")^[a-zA-Z]").ToString();

enter code here

     

The invoiceId works, this gives me the correct number.. I just need help to figure out the value of Status match thing..

Many Thanks,

CodePudding user response:

Try this regex

(?<="Status":").(?=")

It finds a 1 character between quotes (") right after "Status": string. If Status attribute can have a value with more than 1 character add plus ( ) sign after dot (.)

(?<="Status":"). (?=")

If you want to restrict symbols to match you can change dot (.) to applicable symbols. For example:

(?<="Status":")\w(?=")

or

(?<="Status":")[A-Z](?=")

Code in C#

string input = "some text before \"Status\":\"I\", some text after";
string pattern = "(?<=\"Status\":\")[A-Z](?=\")";
Match match = Regex.Match(input, pattern);
if (match.Success)
    Console.WriteLine(match.Value);
  • Related