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.