Regex Match.NextMatch()用于不一致的字符串

本文关键字:不一致 字符串 用于 Match NextMatch Regex | 更新日期: 2023-09-27 17:50:44

我有这样一个输入字符串:

AT+CMGL=4'r'r'n+CMGL: 1,1,,155'r'nDFGDF312GF4J5457JG8J0JGKFJ345G67JHGFGHJ06FD45HJG86J958F4FHSGSDGFH23FJ24HGJH58G4D7D465HDK31HFDJCHGH8V7GD45231DFGF314J567V6GGK4GFJCHGKVGDJX765GHFCJX2X4537CCGHGK9VHJ3C2FJXJCGH'r'n+CMGL: 2,1,,126'r'nDFGDF312GF4J5457JG8J0JGKFJ345G67JHGFGHJ06FD45HJG86J958F4FHSGSDGFH23FJ24HGJH58G4D7D465HDK31HFDJCHGH8V7GD45231DFGF314J567V6GGK4GFJCHGKVGDJX765GHFCJX2X4537CCGHGK9VHJ3C2FJXJCGH'r'n+CMGL: 3,1,,148'r'nDFGDF312GF4J5457JG8J0JGKFJ345G67JHGFGHJ06FD45HJG86J958F4FHSGSDGFH23FJ24HGJH58G4D7D465HDK31HFDJCHGH8V7GD45231DFGF314J567V6GGK4GFJCHGKVGDJX765GHFCJX2X4537CCGHGK9VHJ3C2FJXJCGH'r'n'r'nOK'r'n

我想在这个上做一个正则表达式匹配,提取两个捕获组,并使用NextMatch()方法迭代每个匹配。

我可以实现部分匹配,不包括这个字符串的开始(AT+CMGL=4'r'r'n)和结束('r'nOK'r'n),这对于第一次和最后一次迭代来说是不同的。

这是我用于我能够实现的部分匹配的正则表达式:

'+CMGL: 'd+,'d+,,('d+)''r''n(.*?)''r''n

如何正确的语法正则表达式看起来像得到一个完全匹配?

编辑:我想捕获pdu长度(155)和pdu本身(ndfgdf312gf4j5457jg8j0jgkfj345g67jhgfghj06fd45j86j958fhhsggfh23fj24hgjh58g4d7d465hdk31hfdjchgh8v7gd45231dfgf314j567v6ggk4gfjchgkvgdjx765ghfcjx2x4537ccghgk9vhj3c2fjxjcgh)为每个NextMatch()。

Regex Match.NextMatch()用于不一致的字符串

您的regex是正确的…您只需要使用singleline模式与regex

Regex myRegex = new Regex(yourRegex,RegexOptions.IgnoreCase | RegexOptions.Singleline);
foreach(Match m in myRegex.Matches(yourText))
{
    m.Groups[1].Value;//pdu length
    m.Groups[2].Value;//pdu buffer
}