string result = Regex.Match(yourStr,@"(?s)\d{3}-\d{10}(?=((?!void).){0,20}((?!\d{3}-\d{10}).)*$)").Value;
//void我是小写的,如果你一定要大写,这样 string result = Regex.Match(yourStr,@"(?s)\d{3}-\d{10}(?=((?!VOID).){0,20}((?!\d{3}-\d{10}).)*$)").Value;//如果你不限制大小写string result = Regex.Match(yourStr,@"(?is)\d{3}-\d{10}(?=((?!void).){0,20}((?!\d{3}-\d{10}).)*$)").Value;
貌似在这个字符之后20个字符内无 "VOID" 这个条件没匹配上啊 谢谢啦
疏忽了。 换一下string result = Regex.Match(yourStr,@"(?is)\d{3}-\d{10}(?=(((?!void).){20}|((?!void).){0,19}$)((?!\d{3}-\d{10}).)*$)").Value;再试试
string result = Regex.Match(yourStr,@"(?s)\d{3}-\d{10}(?=((?!VOID).){0,20}((?!\d{3}-\d{10}).)*$)").Value;//如果你不限制大小写string result = Regex.Match(yourStr,@"(?is)\d{3}-\d{10}(?=((?!void).){0,20}((?!\d{3}-\d{10}).)*$)").Value;
换一下string result = Regex.Match(yourStr,@"(?is)\d{3}-\d{10}(?=(((?!void).){20}|((?!void).){0,19}$)((?!\d{3}-\d{10}).)*$)").Value;再试试
string str = @"123-1234567890 098-0987654321 void"; Regex reg = new Regex(@"(?(?=\d{3}-\d{10}.{0,16}void)|\d{3}-\d{10})"); MatchCollection matches = reg.Matches(str);
if (matches.Count == 0 || matches[matches.Count - 1].Value == "")
Console.WriteLine("No Match");
else
Console.WriteLine(matches[matches.Count - 1].Value);
string result = Regex.Match(yourStr,@"(?is)\d{3}-\d{10}(?=(((?!void).){20}|((?!void).){0,19}$))",RegexOptions.RightToLeft).Value;
string str = @"123-1234567890 098-0987654321 void"; Regex reg = new Regex(@"(?i)(?(?=\d{3}-\d{10}.{0,16}void)|\d{3}-\d{10})"); MatchCollection matches = reg.Matches(str);
for (int i = matches.Count - 1; i >= 0; i--)
{
if (matches[i].Value != "")
{
Console.WriteLine(matches[i].Value);
break;
}
}