(?<number>[1-9][0-9]*[a-z]*) - a group "number" which
starts from digit in 1..9 range
can followed by digits in 0..9 range
finally, can followed by letters in a..z range
(?:\s+\k<number>)+ - the very same match "number" repeated
one or more times separated by white spaces
代码:
using System.Linq;
using System.Text.RegularExpression;
...
string text = "Randomstreet 33a 33a or Randomstreet 45 45 apartment 14 14 14";
var result = Regex
.Matches(text, @"(?<number>[1-9][0-9]*[a-z]*)(?:\s+\k<number>)+")
.Select(match => match.Groups["number"].Value)
.ToArray();
Console.Write(string.Join(Environment.NewLine, result));
1条答案
按热度按时间oyxsuwqo1#
如果我们可以假设数字(门牌号,公寓号等)必须从数字
1..9
开始,并且可以以英语小写字母a..z
结束,那么您可以尝试匹配 * 正则表达式 * 模式:模式解释
代码:
输出: