Regex expression matching strings [duplicate] - c#

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 4 years ago.
What input string will the following regex expression match:
[1-4]{0-5}
I know the first part: [1-4] specifies the numerals allowed in the input, and the latter the length? But it does not match any of the expected inputs, such as 1112, 123..

Try the following regex,
It should be :
[1-4]{0,5}
This will match:
empty string = because {0,5} means zero to 5 length of character
1
11
111
1111
11111
2
22
2222
1
12
1234
12344
and it goes on and on but nothing beyond the digit 4, and no length beyond 5
Regex101Demo

The latter is not the length. If it were the length, it would be {0,5} with a comma instead of a -.
Without the comma, it will just match {0-5} literally, so this matches:
1{0-5}
I think you want
[1-4]{0,5}

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

C# Regex, can i set MaxValue instead of MaxLength on my string? [duplicate]

This question already has answers here:
How do I match an entire string with a regex?
(8 answers)
Closed 3 years ago.
I am trying to make a textbox in UWP only allow the following input: 9 digits, followed by a '.' (optional), followed by 2 digits (optional), by using Regex.
So far i have come up with an expression that allows 9 digits, or 9 digits and a '.'
string pattern = #"^(?<Number>([0-9]{0,9})(\.?)([0-9]{0,2}?))";
I want it to accept 123456789 or 123456789.12 as inputs.
But now it only accepts 123456789 or 123456789.
If you want to match 2 optional digits you could make the group optional (?:[0-9]{2})? because [0-9]{0,2} Matches 0, 1 or 2 digits.
The same goes for {0,9} which matches from 0 - 9 times a digit.
To match the whole pattern you should add an anchor $ to assert the end of the string.
If you don't need the capturing groups you could update your pattern to:
^(?<Number>[0-9]{9}\.?(?:[0-9]{2})?)$
Regex demo

Day Validation using Regex [duplicate]

This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 6 years ago.
I'm trying to use Regex to validate the days in a month. With this i am able to validate 01 to 31. How can i also validate 1-31, so that way i can either have 01 to 31 or 1 to 31
(0[1-9]|[12]\d|3[01])
Following will work for you.
(([12]\d)|(3[01])|(0?[1-9]))
I think you didn't understand the way your expression works.
Here it is:
| stands for OR.
Thus you have 3 cases here:
[12]\d
3[01]
0?[1-9]
1 - match either 1 or 2 as the 1st character and \d match any digit (same as [0-9]) as the second character.
2 - match 3 as the 1st digit. And either 0 or 1 as the second one.
3 - match with any digit between 1 to 9 (both included). 0? add an optional 0 as the 1st character.

Regular Expression for data annotations [duplicate]

This question already has answers here:
Strong password regex [duplicate]
(4 answers)
Closed 8 years ago.
How do I write a regular expression for the following
at least 6 characters
at least 1 uppercase
at least 1 lowercase
at least 1 number
at least 1 special character ("#¤%&/( æøå etc.)
I tried the below regex but it isn't working.
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[#+-?$!]).{8,}$
Below regex would satisfy all of your conditions,
^(?=.{6,})(?=.*?[A-Z])(?=.*?[a-z])(?=.*?\d)(?=.*?\W).*$
DEMO
(?=.{6,}) at least 6 characters
(?=.*?[A-Z]) at least 1 uppercase
(?=.*?[a-z]) at least 1 lowercase
(?=.*?\d) at least 1 number
(?=.*?\W) at least 1 non-word character

I want to retrieve numbers from string whose character start from 8 OR 9 OR 6 and followed by 8 digits

I want to retrieve numbers from string whose character start from 8 OR 9 OR 6 and is followed by 8 digits. 
E.G
sting string string  85689252 91585555 65987582  sting 12345678 string
Output should be:
85689252
91585555
65987582
You probably want this regex:
\b[896]\d{7}\b
I assume that the numbers are at least separated by some character other than alphanumeric and _, so I used \b to match word boundary.
If you want to relax the condition, the regex engine must support look ahead and look behind:
(?<!\d)[896]\d{7}(?!\d)
In this case, I just make sure the whole number is not preceded or followed by any digit.
Your question says followed by 8 digits, but it seems you mean the whole number should have 8 digits - so I changed my answer according to your sample input.
Use Regex.Matches to get all the string that matches the regex.

Categories