如何制作一个表达式来匹配以下内容:(包括空格、空格、数字中的其他字符)?示例:
asdasd (S)FS-980/24/BS/02 , asdasd
ffff (S)FS-9525/23/BS/12ss, IP000003587
FSW-952/24/BSB/02
FS-2/24/F/02
FS-4444/23/F/11
我的意思是如果发票号码:
FS -444 4/23/F/11
- 它也更正为:FS-4444/23/F/11
,或FS-4444g/23g/F/1g1
我尝试这样:
...FS(W)-../.{2}/..(.)/\s.{2}
您可以更新模式以首先获得匹配项。然后对这些匹配项进行替换,并删除您不需要的字符。
匹配字符串:
(?:\([A-Z]+\))?FS[A-Z ]*-\d(?:[^\n,()-]*\w)?
要替换匹配部分上不需要的字符:
(?<=^FS-[a-zA-Z0-9]*)[a-z]|[\p{Zs}\ta-z](?=[a-zA-Z0-9\p{Zs}\t]*$)
string patternMatch = @"(?:\([A-Z]+\))?FS[A-Z ]*-\d(?:[^\n,()-]*\w)?";
string patternReplace = @"(?<=^FS-[a-zA-Z0-9]*)[a-z]|[\p{Zs}\ta-z](?=[a-zA-Z0-9\p{Zs}\t]*$)";
string input = @"Fakturasadas : (S)FSK-69/23/GFK/12hjkhjkddddd
(S)FSK-69/23/GFK/ 1 2
(S)FSK-69/23/GFK/12";
var strings = Regex
.Matches(input, patternMatch)
.Cast<Match>().Select(match => {
return Regex.Replace(
match.Value,
patternReplace,
""
);
}
);
foreach (var s in strings) {
Console.WriteLine(s);
}
输出
(S)FSK-69/23/GFK/12
(S)FSK-69/23/GFK/12
(S)FSK-69/23/GFK/12