XXXXXXXXX,XX-XXXXXXX,XXX-XX-XXXX的正则表达式格式 [英] Regex format for XXXXXXXXX , XX-XXXXXXX , XXX-XX-XXXX

查看:296
本文介绍了XXXXXXXXX,XX-XXXXXXX,XXX-XX-XXXX的正则表达式格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



任何人都可以为我提供下面的正则表达式格式。



XXXXXXXXX,

XX-XXXXXXX

XXX-XX-XXXX



我尝试过:



我试过以下
< pre>字符串pattern1 = @\d {9} | \d {3} -\d {2} -\d {4} | \d {3} -\d {6} | \d {5} -\d {4};



 string pattern2 = @[1-9] \d {8} | [1-9] \d?-\ d {7}; 

解决方案

引用:

 \d {9} | \d {3} -\d {2} -\d {4} | \d {3} -\ {6 } | \d {5} -\d {4} 



regEx有什么问题?

建议:使用Debuggex查看漂亮的图表并测试数据。



以下是RegEx文档的链接:

perlre - perldoc.perl.org [ ^ ]

以下是工具的链接帮助构建RegEx并调试它们:

.NET Regex Tester - Regex Storm [ ^ ]

Expresso正则表达式工具 [ ^ ]

RegExr:Learn,Build,&测试RegEx [ ^ ]

在线正则表达式测试器和调试器:PHP,PCRE,Python,Golang和JavaScript [ ^ ]

这个显示RegEx是一个很好的图表,它非常有助于理解RegEx的作用: Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]

此网站还在一个漂亮的图表中显示正则表达式,但无法测试与RegEx匹配的内容: Regexper [


假设C#,你的表达式工作正常:

 string pattern1 = @\d {9} | \d {3} -\d {2} -\d {4} | \d {3} -\ {6} | \d {5} -\d {4}; 
Regex reg =新的正则表达式(pattern1);
if(reg.IsMatch(123456789))Console.WriteLine(9 OK);
if(reg.IsMatch(123-45-6789))Console.WriteLine(3-2-4 OK);
if(reg.IsMatch(123-456789))Console.WriteLine(3-6 OK);
if(reg.IsMatch(12345-6789))Console.WriteLine(5-4 OK);

给予:

 9 OK 
3-2-4 OK
3-6 OK
5-4确定

我唯一的区别就是添加开头和行尾字符:

 string pattern1 = @^ \ d {9} | \d {3} -\d {2} -\d {4} | \d {3} -\d {6} | \d {5} -\d {4} 

;
Regex reg =新的正则表达式(pattern1);

那么它的问题是什么?


Hi Everyone,

Can anyone please provide me the regex format for the below.

XXXXXXXXX,
XX-XXXXXXX
XXX-XX-XXXX

What I have tried:

i tried the below 
<pre>string pattern1= @"\d{9}|\d{3}-\d{2}-\d{4}|\d{3}-\d{6}|\d{5}-\d{4}";


string pattern2 = @"[1-9]\d{8}|[1-9]\d?-\d{7}";

解决方案

Quote:

\d{9}|\d{3}-\d{2}-\d{4}|\d{3}-\d{6}|\d{5}-\d{4}


What is the problem with that regEx?
Advice: use the Debuggex to see a nice graph and test your data.

Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]


First off, make up your mind what language you are using: a JS solution will not be identical to a Java or C# solution.

Assuming C#, your expression works fine:

string pattern1 = @"\d{9}|\d{3}-\d{2}-\d{4}|\d{3}-\d{6}|\d{5}-\d{4}";
Regex reg = new Regex(pattern1);
if (reg.IsMatch("123456789")) Console.WriteLine("9 OK");
if (reg.IsMatch("123-45-6789")) Console.WriteLine("3-2-4 OK");
if (reg.IsMatch("123-456789")) Console.WriteLine("3-6 OK");
if (reg.IsMatch("12345-6789")) Console.WriteLine("5-4 OK");

Gives:

9 OK
3-2-4 OK
3-6 OK
5-4 OK

The only difference I would make is to add start and end of line characters:

string pattern1 = @"^\d{9}|\d{3}-\d{2}-\d{4}|\d{3}-\d{6}|\d{5}-\d{4}


"; Regex reg = new Regex(pattern1);

So what's the problem with it?


这篇关于XXXXXXXXX,XX-XXXXXXX,XXX-XX-XXXX的正则表达式格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆