Reputation: 4620
I searched the whole google to find some ways to verify if the phone number is Romanian but didn't found anything that helps me... I want a Regex validator for the following numbers format:
074xxxxxxx
075xxxxxxx
076xxxxxxx
078xxxxxxx
072xxxxxxx
077xxxxxxx
0251xxxxxx
0351xxxxxx
This is the regex that I've made, but it is not working:
{ "Romania", new Regex("(/^(?:(?:(?:00\\s?|\\+)40\\s?|0)(?:7\\d{2}\\s?\\d{3}\\s?\\d{3}|(21|31)\\d{1}\\s?\\d{3}\\s?\\d{3}|((2|3)[3-7]\\d{1})\\s?\\d$)")}
It doesn't validate the correct numbers format.
More details:
If the number begins with other than the initial ones that I've added, then that number is not valid.
The x should contain any number, but there should not be the same number..like 0000000 1111111 etc.
It can also have the following format (but not mandatory): (072)xxxxxxx
Is there any way of doing this?
I want to implement this to store these numbers in database and check if their format is Romanian. This is the code where I need to add the regex expression...there should be a new Regex named "Romanian"
static IDictionary<string, Regex> countryRegex = new Dictionary<string, Regex>()
{
{ "USA", new Regex("^[2-9]\\d{2}-\\d{3}-\\d{4}$")},
{ "UK", new Regex("(^1300\\d{6}$)|(^1800|1900|1902\\d{6}$)|(^0[2|3|7|8]{1}[0-9]{8}$)|(^13\\d{4}$)|(^04\\d{2,3}\\d{6}$)")},
{ "Netherlands", new Regex("(^\\+[0-9]{2}|^\\+[0-9]{2}\\(0\\)|^\\(\\+[0-9]{2}\\)\\(0\\)|^00[0-9]{2}|^0)([0-9]{9}$|[0-9\\-\\s]{10}$)")},
};
Upvotes: 1
Views: 3955
Reputation: 43743
If I understand the rules correctly, this pattern should work:
^(?<paren>\()?0(?:(?:72|74|75|76|77|78)(?(paren)\))(?<first>\d)(?!\k<first>{6})\d{6}|(?:251|351)(?(paren)\))(?<first>\d)(?!\k<first>{5})\d{5})$
So, you could add it to your code like this:
static IDictionary<string, Regex> countryRegex = new Dictionary<string, Regex>()
{
{ "USA", new Regex("^[2-9]\\d{2}-\\d{3}-\\d{4}$")},
{ "UK", new Regex("(^1300\\d{6}$)|(^1800|1900|1902\\d{6}$)|(^0[2|3|7|8]{1}[0-9]{8}$)|(^13\\d{4}$)|(^04\\d{2,3}\\d{6}$)")},
{ "Netherlands", new Regex("(^\\+[0-9]{2}|^\\+[0-9]{2}\\(0\\)|^\\(\\+[0-9]{2}\\)\\(0\\)|^00[0-9]{2}|^0)([0-9]{9}$|[0-9\\-\\s]{10}$)")},
{ "Romania", new RegEx(@"^(?<paren>\()?0(?:(?:72|74|75|76|77|78)(?(paren)\))(?<first>\d)(?!\k<first>{6})\d{6}|(?:251|351)(?(paren)\))(?<first>\d)(?!\k<first>{5})\d{5})$")}
};
Here is the meaning of the pattern:
^
- Matches must start at the beginning of the input string(?<paren>\()?
- Optionally matches a (
character. If it is there, it captures it in a group named paren
0
- The number must start with a single 0
(?:
- Begins an non-capturing group for the purpose of matching one of two different formats(?:72|74|75|76|77|78)(?(paren)\))(?<first>\d)(?!\k<first>{6})\d{6}
- The first format
(?:72|74|75|76|77|78)
- The next two digits must be 72
, 74
, 75
, 76
, 77
, or 78
(?(paren)\))
- If the opening (
exists, then there must be a closing )
here(?<first>\d)
- Matches just the first of the ending seven digits and captures it in a group named first
(?!\k<first>{6})
- A negative look-ahead which ensures that the remaining six digits are not the same as the first one\d{6}
- Matches the remaining six digits|
- The or operator(?:251|351)(?(paren)\))(?<first>\d)(?!\k<first>{5})\d{5}
- The second format
(?:251|351)
- The next three digits must be 251
or 351
.(?(paren)\))
- If the opening (
exists, then there must be a closing )
here(?<first>\d)
- Matches just the first of the ending six digits and captures it in a group named first
(?!\k<first>{5})
- A negative look-ahead which ensures that the remaining five digits are not the same as the first one\d{5}
- Matches the remaining five digits)
- Ends the non-capturing group which specified the two potential formats$
- The match must go all the way to the of the input stringUpvotes: 3
Reputation: 2845
Try this one: ^(?=0[723][2-8]\d{7})(?!.*(.)\1{2,}).{10}$
- The negative lookahead (?!...)
is testing the repeating characters
I use http://regexr.com/ to test
Upvotes: 0