I still don't understand regular expression [duplicate] - c#

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 8 years ago.
I have this piece of code but have no idea what it is supposed to be matching. I have looked at many different sites to try and learn the keywords, but I just don't understand regex.
string key = #"^(.*)\s*=\s*(.*)\s*$";
Match value = Regex.Match(line, key);

This looks for the start of a line (^), finds any number of characters ((.*)), followed by some whitespace (\s*) an equal sign (=) some more whitespace (\s*) and any number of characters ((.*)) and the end of line ($)
Some valid example lines:
a=a
abc = xyz
value=5
etc

Related

Need Regular expression for the following [duplicate]

This question already has answers here:
Regular expression to validate numbers separated by commas or dashes [closed]
(2 answers)
Closed 2 years ago.
I have to write the Regex expression which accepts - and only numbers either single four digit number or two 4 digit numbers seperated by hyphen as shown below
2751, 2759-2764, 2766-2774, 2776-2777, 2890-2897
3945-3974, 3979, 3984-3999
I have used this Regex ^[0-9_,]+ but this line Regex.IsMatch(line, #"^[0-9_,]+$") returns false.
Regards,
Nagasree
The pattern that you tried is not matching as there is no hyphen or space in the character class. But when you would add those, the pattern still does not take any format into account.
You could match 4 digits with optional hyphen and 4 digits part. Then repeat that preceded by a space:
^[0-9]{4}(?:-[0-9]{4})?(?:, [0-9]{4}(?:-[0-9]{4})?)*$
Regex demo
var s = "2751, 2759-2764, 2766-2774, 2776-2777, 2890-2897";
Console.WriteLine(Regex.IsMatch(s, #"^[0-9]{4}(?:-[0-9]{4})?(?:, [0-9]{4}(?:-[0-9]{4})?)*$"));
Output
True

How to get all possible Regex Matches [duplicate]

This question already has answers here:
C# Code to generate strings that match a regex [closed]
(4 answers)
Closed 3 years ago.
Based off a regex string I would like to get a list of all the possible strings that would match the regex.
Example:
Given a regex string like...
^(en/|)resources/case(-| )studies/
I want to get a list of all the possible strings that would match the regex expression. Like...
^en/resources/case-studies/
or
^/resources/case-studies/
or
^en/resources/case studies/
or
^/resources/case studies/
Thank you
Note that in regex ^ denotes the beginning of the line. You must escape it
Try
\^(en)?/resources/case(-|\s)studies/
explanation:
\^ is ^ escaped.
(en)? is optionally en, where ? means zero or one times.
/resources/case the text as is.
(-|\s) minus sign or white space.
studies/ the text as is.
See: https://dotnetfiddle.net/PO4wKV

Regex expression to verify that a string only contains letters, numbers, underscores and dashes [duplicate]

This question already has answers here:
Reference - What does this regex mean?
(1 answer)
What do ^ and $ mean in a regular expression?
(2 answers)
Closed 3 years ago.
I've already read this here and I still have got some questions. I will be very grateful if you can help me to solve them. I'm trying to compose a RegEx to verify that a string contains only letters, numbers, underscores and hyphens. Firstly, when I tried to do it (without Google-search) I did this #"[A-Za-z0-9_-]". After I made some research I found that it should be #"^[a-zA-Z0-9_-]$" where:
^ asserts position at start of a line
$ asserts position at the end of a line
My question is why we should insert these symbols? And my other question is why the string "jeffbutt" (with yellow in the screenshot) doesn't match?

verify that string matches a regex pattern [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
Is there a way to verify with Regex that a string matches a patter like so:
1.001
1.002
15.001
Where the pattern is:
any number followed by
dot character
followed by three (3) numbers
Given that patters things like this would fail:
A.001
1.0001
1,001
...
Any help is appreciated.
This regex should work:
^\d+\.\d{3}$

RegEx to match only {n} digits and not much or less surrounded by whatever [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 3 years ago.
I'm trying to match only {n} digits not more or less and may be surrounded by characters or special symbols
example:
suppose the {n} = {14}
*12345678901234*300 OK
12345678901234x21 OK
*123456789012345*300 NOT OK
12345678901234 OK
123456789012345 NOT OK
You could use negative lookarounds to assert what is directly on the left and right is not a digit and match 14 digits:
(?<!\d)\d{14}(?!\d)
.NET regex demo
(?:^|\D)(\d{14})(?:\D|$)
Here is a Live Demo

Categories