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
Related
This question already has answers here:
How to validate phone numbers using regex
(43 answers)
US Phone Number Verification
(13 answers)
Regex to match all us phone number formats
(10 answers)
Regular expression to match US phone numbers
(5 answers)
Closed 3 years ago.
I am new to regex and I am burning hours just to get it right. I need to allow these patterns
NPA-XXX-XXXX
(NPA) XXX-XXXX
NPAXXXXXXX
where NPA = numbers 0 to 9
and X is also any number 0 to 9
so this is valid
123-456-7890
1234567890
(123) 456-7890
but not this
(123)-456-7890 // because there is a dash after closing parenthesis
(123)456-7890 // because there is no space after closing parenthesis
QWE-456-7890 // because there are one or more alpha characters
I use this
Regex r = new Regex(#"^?\(?\d{3}?\)??-??\(?\d{3}?\)??_??\(?\d{4}?\)??-?$");
from System.Text.RegularExpressions
What would be the regular expression that would match the valid?
One way to look at this:
all numbers end in XXX-XXXX, with the - being optional, so that's d{3}\-?\d{4}
numbers start with either (NPA) or NPA-, with the - being optional, so that's \(\d{3}\) (ending in a space) or \d{3}
Since you can 'or' parts of a regex together with (a|b), the whole regex becomes:
(\(\d{3}\) |\d{3}\-?)\d{3}\-?\d{4}
Note that \( escapes the parenthesis, so it's not seen as part of the regex language, but an actual character you want to match.
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:
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
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
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}$