我的字串如下所示,帶有 3 個 SSN 號碼。
Mr Tim Tom SSN 123-45-6789 (United States); alt. SSN 345-45-6576 (United States) SSN 22-1234567-8 (Philippines)
我正在嘗試使用 Regex 和 C# 獲取 SSN
O/P:
123-45-6789
345-45-6576
22-1234567-8
要么
O/P:
SSN 123-45-6789
SSN 345-45-6576
SSN 22-1234567-8
我無法通過以下邏輯獲得所有 SSN
var matches = Regex.Matches("Mr Tim Tom...<<above string>>", "SSN (. )", RegexOptions.Singleline);
請提供任何幫助
uj5u.com熱心網友回復:
嘗試使用正則運算式模式:
SSN (\d (?:-\d ){2})
C#代碼:
var input = "Mr Tim Tom SSN 123-45-6789 (United States); alt. SSN 345-45-6576 (United States) SSN 22-1234567-8 (Philippines)";
MatchCollection matches = Regex.Matches(input, @"SSN (\d (?:-\d ){2})");
foreach (Match match in matches)
{
Console.WriteLine("SSN: {0}", match.Groups[1].Value);
}
這列印:
SSN: 123-45-6789
SSN: 345-45-6576
SSN: 22-1234567-8
uj5u.com熱心網友回復:
那應該作業:
SSN\s[\d\-]{9,}
SSN #literal string
\s #single whitespace character
[ ] #matching group, match any symbol between the brackets
\d #match any number
\- #match a hyphen, escaped
{9,} #quantifier, match previous expression between 9 and unlimited times
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/449944.html
