XXXXXXXXX,XX-XXXXXXX,XXX-XX-XXXX ex(123456789,123-12-1234,12-1234567)的正则表达式格式) [英] Regex format for XXXXXXXXX , XX-XXXXXXX , XXX-XX-XXXX ex (123456789, 123-12-1234, 12-1234567))
问题描述
大家好,
能否为您提供以下正则表达式。
In应用程序,用户将提供描述如果描述包含SSN或EIN('049-90-1935',149901935,14-9901935),则应替换为xxxxxxxx。
以下是要替换的有效格式。
049-90-1935
ANR049-90-1935
049- 90-1935BIH
049901935
14-9901935BIN
格式无效:
0141234567897无效。不应该更换。序列长度超过9位。
0141234无效。不应该更换。序列短于9位。
123456789abc123456789无效
Hi Everyone,
Can you please provide a regular expression for the below.
In the application, the user will provide a description If the description contains an SSN or EIN ('049-90-1935',149901935,14-9901935), should replace with "xxxxxxxx".
Below are the valid formats to replace.
049-90-1935
ANR049-90-1935
049-90-1935BIH
049901935
14-9901935BIN
Invalid formats:
0141234567897 Invalid. Should not be replaced. The sequence is longer than 9 digits.
0141234 Invalid. Should not be replaced. The sequence is shorter than 9 digits.
123456789abc123456789 INvalid
(1)The data consists of 9 digits only.
(2)The data is delimited by either a SPACE or non-digit character.
(3)Embedded space(s) separating digits should be ignored.
(4)For SSNs, the acceptable formats are
a.xxx-xx-xxxx or
b.xxxxxxxxx where x represents a digit (number from 0 thru 9)
(5)For EINs, the acceptable formats are
a.xx-xxxxxxx or
b.xxxxxxxxx where x represents a digit (number from 0 thru 9)
我尝试了什么:
What I have tried:
I tried with the following regular expression.
<pre>string ssnPattern = @"[^0-9](?<grpA>\d{9}) |[^0-9](?<grpB>\d{3}-\d{2}-\d{4}) | [^0-9](?<grpC>\d{2}-\d{7}) ";
var matches = Regex.Matches(description, ssnPattern).Cast<Match>().Select(m => m).ToList();
if (matches != null && matches.Count > 0)
{
redactionText = Regex.Replace(content, ssnPattern, m =>
{
string val = "";
if (m.Groups["grpA"] != null && m.Groups["grpA"].Value != "")
{
val = m.Value.Replace(m.Groups["grpA"].Value, req.ReplacementText);
matchingItems.Add(m.Groups["grpA"].Value);
}
if (m.Groups["grpB"] != null && m.Groups["grpB"].Value != "")
{
val = m.Value.Replace(m.Groups["grpB"].Value, req.ReplacementText);
matchingItems.Add(m.Groups["grpB"].Value);
}
if (m.Groups["grpC"] != null && m.Groups["grpC"].Value != "")
{
val = m.Value.Replace(m.Groups["grpC"].Value, req.ReplacementText);
matchingItems.Add(m.Groups["grpC"].Value);
}
return val;
});
}
Unable to resolve all the scenarios. Can anyone please provide a solution.
推荐答案
(2)The data is delimited by either a SPACE or non-digit character.
(3)Embedded space(s) separating digits should be ignored.
那你如何解决类似0499019 35的问题
?
(2)数据由任一分隔一个空格或非数字字符。
(2)The data is delimited by either a SPACE or non-digit character.
你的问题是你的例子没有遵循规则2.
You have the problem that your examples are not following rule 2.
(3)应忽略分隔数字的嵌入空格。
(3)Embedded space(s) separating digits should be ignored.
此规则将使RegEx用法变得复杂。
只需几个有趣的链接来帮助构建和调试RegEx。
以下是RegEx文档的链接:
< a href =http://perldoc.perl.org/perlre.html> perlre - perldoc.perl.org [ ^ ]
以下是帮助构建RegEx并调试它们的工具的链接:
.NET Regex Tester - Regex Storm [ ^ ]
Expresso正则表达式工具 [ ^ ]
RegExr :学习,构建,和测试RegEx [ ^ ]
在线正则表达式测试器和调试器:PHP,PCRE,Python,Golang和JavaScript [ ^ ]
这个显示RegEx是一个很好的图表,它非常有助于理解RegEx的作用: Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]
此网站还在一个漂亮的图表中显示正则表达式,但无法测试与RegEx匹配的内容: Regexper [ ^ ]
This rule will really complicate RegEx usage.
Just a few interesting links to help building and debugging RegEx.
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[^]
这篇关于XXXXXXXXX,XX-XXXXXXX,XXX-XX-XXXX ex(123456789,123-12-1234,12-1234567)的正则表达式格式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!