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.
Related
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 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:
What do ^ and $ mean in a regular expression?
(2 answers)
Closed 3 years ago.
I'm trying to design a RegEx in C# to match 4 digits and 6 characters in a string.
string str = "Hello World! ABCD112233 fsdf sdfsdf 234324 fdsfds 4234 efwedf34ref dfsdf34f34f";
that matches only ABCD112233 in the above string.
Regex regex = new Regex("^[A-Za-z]{4}[0-9]{6}$", RegexOptions.Multiline);
How do I solve this problem?
You would use the {n} quantifier to match a certain number of characters like so: [A-Za-z]{4}[0-9]{6}. This will match 4 letters (A-Z, a-z) and then 6 digits (0-9).
Note: don't use ^ and $ at the start and end as then it will only match if the whole word matches the regex.
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:
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