简体   繁体   English

正则表达式以匹配所有罗马尼亚电话号码

[英]Regex to match all Romanian phone numbers

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: 我在整个Google上进行了搜索,以找到一些方法来验证电话号码是否为罗马尼亚语,但没有找到任何对我有帮助的方法...我想要一种正则表达式验证器,用于以下数字格式:

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. x应该包含任何数字,但不应有相同的数字。例如0000000 1111111等。

  • It can also have the following format (but not mandatory): (072)xxxxxxx 它还可以具有以下格式(但不是强制性的):(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" 这是我需要添加正则表达式的代码...应该有一个名为“ 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}$)")},
};

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 (?<paren>\\()? -可选地匹配一个(字符。如果存在,则将其捕获到名为paren的组中
  • 0 - The number must start with a single 0 0数字必须以单个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)(?(paren)\\))(?<first>\\d)(?!\\k<first>{6})\\d{6} -第一种格式
    • (?:72|74|75|76|77|78) - The next two digits must be 72 , 74 , 75 , 76 , 77 , or 78 (?:72|74|75|76|77|78) -接下来的两个数字必须是7274757677 ,或78
    • (?(paren)\\)) - If the opening ( exists, then there must be a closing ) here (?(paren)\\)) -如果在此处有开头(存在,则必须有一个结尾)
    • (?<first>\\d) - Matches just the first of the ending seven digits and captures it in a group named first (?<first>\\d) -仅匹配结尾七个数字中的第一个,并将其捕获到名为first的组中
    • (?!\\k<first>{6}) - A negative look-ahead which ensures that the remaining six digits are not the same as the first one (?!\\k<first>{6}) -负前瞻,可确保其余六个数字与第一个数字不同
    • \\d{6} - Matches the remaining six digits \\d{6} -匹配其余六位数字
  • | - The or operator - 运算子
  • (?:251|351)(?(paren)\\))(?<first>\\d)(?!\\k<first>{5})\\d{5} - The second format (?:251|351)(?(paren)\\))(?<first>\\d)(?!\\k<first>{5})\\d{5} -第二种格式
    • (?:251|351) - The next three digits must be 251 or 351 . (?:251|351) -接下来的三位数必须为251351
    • (?(paren)\\)) - If the opening ( exists, then there must be a closing ) here (?(paren)\\)) -如果在此处有开头(存在,则必须有一个结尾)
    • (?<first>\\d) - Matches just the first of the ending six digits and captures it in a group named first (?<first>\\d) -仅匹配结尾六位数字中的第一位并将其捕获到名为first的组中
    • (?!\\k<first>{5}) - A negative look-ahead which ensures that the remaining five digits are not the same as the first one (?!\\k<first>{5}) -负前瞻,可确保其余五个数字与第一个数字不同
    • \\d{5} - Matches the remaining five digits \\d{5} -匹配其余五位数字
  • ) - Ends the non-capturing group which specified the two potential formats ) -结束指定两种可能格式的非捕获组
  • $ - The match must go all the way to the of the input string $ -匹配项必须一直到输入字符串的

Try this one: ^(?=0[723][2-8]\\d{7})(?!.*(.)\\1{2,}).{10}$ - The negative lookahead (?!...) is testing the repeating characters 试试这个: ^(?=0[723][2-8]\\d{7})(?!.*(.)\\1{2,}).{10}$ -负前瞻(?!...)正在测试重复字符

I use http://regexr.com/ to test 我使用http://regexr.com/进行测试

这符合您的示例:

0(([7][456728])|([23]51)).*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM