This question already has answers here:
How can I deserialize JSON with C#?
(19 answers)
Closed 5 years ago.
Just a quick one, I'm probably just blind. I'm using regex to match bits of JSON data. I have this line:
String ErrorMessage = new Regex("\"message\":\"\\w+\"").Match(response).Value;
Which should hopefully match something like this:
"message":"This is a messsage"
But it isn't matching it at all, I know it's in there though.
Thanks in advance.
EDIT: While this might not be best practice, it's the first time that I've used both regex and JSON, and so didn't want to use any external APIs or libraries. I'll be sure to use something better in the future :)
This is not a duplicate, as I am not asking how to parse JSON, I'm simply asking why a regex doesn't match a pattern. Thanks.
Thanks for the answers!
The \w character class does not include the space character.
Related
This question already has answers here:
Regex Match all characters between two strings
(16 answers)
Closed 3 years ago.
Though this is probably a duplicate, i haven't been able to make this work after reading similar questions. Looking for help please with this homework, i'm a newbie at programming.
I'm working on a program in C#. I have a text that contains for example this sentence: "My homework version V0.90 from". I need to extract "V0.90", and that may vary from anything between V0.90 to V2.00. It is always surrounded by "My homework version " and " from", i need to extract whatever is between that. This is what i've tried:
string RetRegMatch;
Match Match1 = Regex.Match(Friends[a], #"My homework version (.+) from", RegexOptions.IgnoreCase);
RetRegMatch = Match1.Value;
But i'm geeting as a result in Match1.Value this: "My homework version V0.90 from", and i only want the (+.) part to be in Match1.Value. That is, i would like to have in Match1.Value this: "V0.90". How can i do this?
The part you need should be in the capture group (the part you put between ()). Try accessing it with
Match1.Groups[1].Value
This question already has answers here:
Returning only part of match from Regular Expression
(4 answers)
Closed 4 years ago.
I have a responce string
"c=2020&action=approvecomment&_wpnonce=7508ac918a' data-wp-lists='dim:the-comment-list:comment-2020:unapproved:e7e7d3:e7e7d3:new=approved"
Im trying to extract 2020 and 7508ac918a. I dont understand how I must use regex with substrings in C#, simple regex like
c=(\d+)&action=approvecomment&_wpnonce=(.*?)' .+new=approved.
In Regex, you can create match groups
They look like this (?.+?)
So your _wpconce part could become something like this (?.*?)
Then you can grab each group individually for example
Match result = myRegex.Match(someString);
soneOtherString = result.Groups["GROUPNAME"].Value;
I use Regex101 to build and test my regex. (Whoever made that site deserves a crown with shinny stones on it!! :)
https://regex101.com/
Hope this helps
This question already has answers here:
Regular expression for GST Identification Number (GSTIN)
(14 answers)
Closed 5 years ago.
How to Validate GSTIN Number using regex.
eg.11ABCDE1234L1Z1
My current regex is:
^([a-zA-Z0-9_\.\-])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$
Last digit is check digit. There is not much clarity on the internet on its validation - based on some real GSTIN examples - its alphanumeric - 0-9A-Z
The correct regex is therefore -
^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$
This seems to be the most accepted Regex for GSTIN Number.
/^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$/
Based on the requirements in the comment section, this is what I came up with.
([01]\d|[2][0-79]|[3][0-7])[A-Z]{5}\d{4}[A-Z]{1}\d[A-Z]{1}\d
how about this one?
\d{2}[A-Z]{5}\d{4}L\dZ\d
System.Text.RegularExpressions.Regex rPan =
new System.Text.RegularExpressions.Regex("^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[0-9]{1}[Z]{1}[0-9]{1}$");
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 6 years ago.
I'm converting a lot of code from legacy to maintainable and I'm creating a list of regex we can use to do all the pages quickly and the same. My regex skills are that of a child running with a knife...its not great. I've looked up a lot of different ways to only find the first set but I can't seem to get it to work. Can anyone solve this specific problem for me?
Here is the regex search and replace I'm using.
regex: (rs.*)\.Fields\[\"(\w+)\"\].Value
replace: $1.GetValue<object>("$2")
Works
code to search: ...rsProducts.Fields["Price"].Value...
result: rsProducts.GetValue<object>("Price")
This, as I want it to, finds the rs (recordset) of something and changes the way that we extract the value to use an extension method.
Does Not Work
code to search: ...rsProducts.Fields["Price"].Value + rsProducts.Fields["Price2"].Value...
result: rsProducts.Fields["Price"].Value + rsProducts.Fields["Price2"].Value
should be: rsProducts.GetValue<object>("Price") + rsProducts.GetValue<object>("Price2")
In this case the search does match 2 distinct instances but instead it matches the entire line. Here's a pic from regexr.com.
// sorry I don't have the reputation to post the image as an image but heres the
Link to Example Image
You're not dealing handling the case for the + between the two.
(rs.*?)\.Fields\[\"(\w+)\"\].Value
This question already has answers here:
Is there a way to test if a string is an MD5 hash?
(6 answers)
Closed 8 years ago.
I've got a problem with checking whether the string is md5 or not. I know that it should contain "0123456789ABCDEF" and its length equals to 32. I know that regex is a good way to check it, but I have no idea to use it. I tried searching on msdn but nothing cleared my mind. Can anyone help me?
Use Regex.IsMatch and the regex [0-9a-f]{32}:
if (Regex.IsMatch(string, #"[0-9a-f]{32}"), RegexOptions.IgnoreCase))
//String is MD5