This question already has answers here:
C# Regex to match the word with dot
(3 answers)
Regex plus vs star difference? [duplicate]
(9 answers)
Closed 3 years ago.
I have some validation code that checks a string against a regular expression.
Regex regex = new Regex(RegexPattern);
if (!regex.IsMatch(value))
{
errorMessage = "The data is not in the correct format.";
return false;
}
If I set the regular expression pattern to ^[0-9]*.[0-9]*.[0-9]*.[0-9]*$, it correctly accepts 1.0.0.0; however, it also accepts 1.0.0..
How can I modify the pattern so that 1.0.0.0 is accepted but 1.0.0. is rejected?
[0-9]* means 0 or more occurrence of [0-9]
[0-9]+ means 1 or more occurrence of [0-9]
^[0-9]*.[0-9]*.[0-9]*.[0-9]*$
Change * to +:
^[0-9]+.[0-9]+.[0-9]+.[0-9]+$
Just a slight misunderstanding about * and +. The former accepts either no occurrences or more (>=0), the latter only matches if such vocabulary occurs at least once (>=1).
^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$
I usually also escape the dot for safety, not entirely sure if it's necessary but I still do it. :)
You can use this pattern:
^\d+\.\d+\.\d+\.\d+$
Explanation:
^ - begin of string
\d - any digit
+ - at least one char
\. - exactly a dot char
$ - end of string
By the way, your input looks like an IP address. If so you can modify your reges like this:
^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$
{1,3} means: 'from 1 to 3 chars'
Demo: regex101
To match 4 sets of digits separated by a period
^\d+(?:\.\d+){3}$
Related
This question already has answers here:
How do I make part of a regex match optional?
(2 answers)
Order of regular expression operator (..|.. ... ..|..)
(1 answer)
Closed 1 year ago.
I am trying to match a substring of "Number" or "Number(s)" in a string using a single Regex. However, I can get them to match individually, but not together.
Individually,
'Number' can match the word number
'Number[(]s[)]' can match Number(s).
However, if I put them together and do "Number|Number[(]s[)]" it is not matching for (s) of "Number(s)".
What I have tried:
1: Put \b boundary around the second string, doesn't work.
2: Use \ to escape, but C# yells at me for unrecognized escape sequence, so I opted out of this option
I know that I can use two regex to do what I want, but I wanted to understand what is wrong here and learn.
Number|Number[(]s[)] wont match Number(s) because it's first part "Number" matches it.
Try change the pattern part order: Number[(]s[)]|Number. This will try to match first with the string with parentheses and if it can't it will try the short form.
Also the pattern should be: Number\(s\)|Number
The unrecognized escape error message comes because if you want this pattern written as a string literal you must escape the backslash signs: "Number\\(s\\)|Number".
This question already has answers here:
Regex escape with \ or \\?
(5 answers)
Closed 2 years ago.
I tried to use regexes in C#
^(?=.*\d)(?=.*[a - z])(?=.*[A - Z])(?=.*[!##$%^*])(?=.*[a-zA-Z]).{6,20}$
but \d comes as an error if i put [0-9] instead it wont work as desired
This should check the string has a uppercase, lowercase, symbol and a number
You should use [0-9]. Probably it is more correct... \d will catch non-european digits like рен (it is a Devanagari digit).
For the reason:
you probably wrote:
var rx = new Regex("\d");
But in this way the \d is an escape sequence of the string instead of being a regex.
Write
var rx = new Regex(#"\d");
to deactivate the escape expansion of strings.
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
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I want to make sure a first name field has at least one alphanumeric character and also allow spaces and dashes.
**VALID**
David
Billie Joe
Han-So
**INVALID**
-
Empty is also invalid
To ensure the dashes and spaces happen in legitimate places, use this:
(?i)^[a-z]+(?:[ -]?[a-z]+)*$
See demo.
(?i) puts us in case-insensitive mode
^ ensures we're at the beginning of the string
[a-z]+ matches one or more letters
[ -]?[a-z]+ matches an optional single space or dash followed by letters...
(?:[ -]?[a-z]+)* and this is allowed zero or more times
$ asserts that we have reached the end of the string
You mentioned alphanumeric, so in case you also want to allow digits:
(?i)^[a-z0-9]+(?:[ -]?[a-z0-9]+)*$
use this pattern
^(?=.*[a-zA-Z])[a-zA-Z -]+$
Demo
oh, for alphanumeric use
^(?=.*[a-zA-Z0-9])[a-zA-Z 0-9-]+$
This question already has answers here:
Regular expression to enforce complex passwords, matching 3 out of 4 rules
(4 answers)
Closed 8 years ago.
My password strength criteria is as below :
Character length: 8 -> 24
Contains least 3 of following types:
Uppercase letter [A-Z]
Lowercase letter [a-z]
Numeric [0-9]
Special character ~`!##$%^&*()-_=+[{]}\|;:'"<,>.?/
Can anyone help me to make regular expression and explain. Thanks.
It would be very difficult to do in one regex. Personally, I'd check for each case separately and count if you've got three matches. It would be easier to read and maintain.
So match [A-Z] then [a-z] then [0-9] and finally [~`!##$%^&*()-_=+[{]}\|;:'"<,>.?/]
If you end up with a match in three of the tests that's success.
Use this Regex:
(?=.{8,24})(?=.*\d)(?=.*[A-Za-z])(?=.*[~`!##\$%\^&\*()_=\+[{\]}\|;:'"<,>\.\?/]).*
Debuggex Demo