Matching 10 or 12 digits with Regex in C# [duplicate] - c#

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})$

Related

Regex Get only last 32 random digit and numbers [duplicate]

This question already has answers here:
Regular Expression only match if String ends with target
(3 answers)
Closed 2 years ago.
Hello I'm having a little problem with getting the last 32 random digits and numbers (ID)
here's the example links
LINK 1: https://www.testurl/9c47c746-21e6-457a-a557-e5f93bdfff05/056c623a-9327-40d2-8ed7-c349dac76f0d
LINK 2: https://www.testurl/a08e9b68-e65b-4beb-b112-fd4360915e82
here's the Regex I'm using:
(it's only match the first 32 random digits and numbers which is from LINK 2)
(\w{8}-\w{4}-\w{4}-\w{4}-\w{12})
I also want to get only the "056c623a-9327-40d2-8ed7-c349dac76f0d" from the "LINK 1" after the slash
Thank you #41686d6564
answer:
\b\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$

Regular expression for US phone number [duplicate]

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.

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

RegEx to match only {n} digits and not much or less surrounded by whatever [duplicate]

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

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

Categories