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.
Related
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 1 year ago.
Can anyone tell me what this kind of Regular Expression is doing?
(\s*(\s*#\s*(?\d+)\s*))
If I test it an add whitespaces it says there is no match.
Can somebody give me an example? :)
That regular expression is not correct. Try removing the ? character.
(\s*(\s*#\s*(\d+)\s*))
And this expression matches any number that has an # in front of it. For example, in the next lines, it matches lines 1,2,5 and 6.
#995
#995
995
995
#995 9
#654 233
233
You can test it in the following link:
http://regexstorm.net/tester?p=%28%5cs*%28%5cs*%23%5cs*%28%5cd%2b%29%5cs*%29%29&i=++%23995%0d%0a%23995%0d%0a++995%0d%0a995%0d%0a%23995+9%0d%0a++%23654+233%0d%0a233
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
This question already has answers here:
Regex to match 10 or 12 digits only
(3 answers)
Closed 4 years ago.
I'm new to C# and coming from PHP background.
I wanted to validate only 10 and 12 digits only with following regex statment.
I got correct regex. But still I'm getting invalid number error if tested
Pleae point me out where I do the mistake here.
Code is compiled without any issues.
// To validate 10 digits or 10 digits only
string regxstr = "^(?=[0-9]*$)(?:.{10}|.{12})$";
if (Regex.IsMatch(Data.number, regxstr))
{
return "Valid number";
}
else {
return "Invalid number";
}
Your testing data does not consists of only digits.
You could simplify your regex to match 10 digits \d{10} with and optional group that matches 2 digits (?:\d{2})?.
^\d{10}(?:\d{2})?$
Demo
I wonder why don't you use simple regular expression for this like:
^(\d{10}|\d{12})$
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}
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