1

我有这个输入字符串:

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

正则表达式的正确语法如何获得完全匹配?

EDIT: I would like to capture the pdu length (155) and the pdu itself (nDFGDF312GF4J5457JG8J0JGKFJ345G67JHGFGHJ06FD45HJG86J958F4FHSGSDGFH23FJ24HGJH58G4D7D465HDK31HFDJCHGH8V7GD45231DFGF314J567V6GGK4GFJCHGKVGDJX765GHFCJX2X4537CCGHGK9VHJ3C2FJXJCGH) for each NextMatch().

4

1 回答 1

2

regex是正确的..你只需要使用singleline正则表达式的模式

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
}
于 2013-01-04T10:15:35.143 回答