Regex regexObj = new Regex(@"(\b\d+\.\d{2}\b).*?(\b\d{11}\b)");
Match matchResults = regexObj.Match(subjectString);
while (matchResults.Success) {
for (int i = 1; i < matchResults.Groups.Count; i++) {
Group groupObj = matchResults.Groups[i];
if (groupObj.Success) {
// matched text: groupObj.Value
// match start: groupObj.Index
// match length: groupObj.Length
}
}
Объяснение:
( # Match and capture the following:
\b # Assert that the match starts at a "word boundary"
\d+ # Match one or more digits
\. # Match a .
\d{2} # Match exactly two digits
\b # Assert that the number ends here
) # End of first capturing group
.*? # Match any number of intervening characters; as few as possible
( # Match and capture...
\b # Word boundary
\d{11} # Exactly 11 digits
\b # Word boundary
) # End of match
Группа № 1 будет содержать десятичное число, группа № 2 будет содержать 11-значное число.
A«граница слова» - это позиция между буквенно-цифровым символом и не буквенно-цифровым символом, поэтому она совпадает только в начале или в конце слова или числа.
Это гарантирует, что числа типа 12.3456
не будут совпадать;с другой стороны, необходимо, чтобы числа были разделены пробелами, пунктуацией или другими символами, отличными от букв.Например, в number12.34
регулярное выражение не будет соответствовать 12.34
.