Search a string for certain numbers
开发者_Python百科I'm trying to come up with a lightning fast solution to find portions in a string. Here is a Sample string:
"PostLoad successful! You transferred amount of 17.00 Rs to 03334224222. Now use PostLoad by dialing 123. PostLoad on SMS will end on 01-03-2011."
Objective: Need to retrieve the bold values: Amount and Cell Number. The string contents change slightly but the cell number will always be a 11 digit. The amount is always with two decimal precision. Any suggestions using C# and RegEx?
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
}
}
Explanation:
( # 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
Group #1 will contain the decimal number, group #2 will contain the 11-digit number.
A "word boundary" is the position between an alphanumeric character and a non-alphanumeric character, so it only matches at the start or end of a "word or number".
This ensures that numbers like 12.3456
will not be matched; on the other hand, it is necessary for the numbers to be delimited by whitespace, punctuation or other non-alnum characters. For example, in number12.34
the regex would not match 12.34
.
Here is a conversion to VB.Net. I hope I got it right.
Dim regexObj As New Regex("(\b\d+\.\d{2}\b).*?(\b\d{11}\b)")
Dim matchResults As Match = regexObj.Match(lActualSenderMessage)
While matchResults.Success
For i As Integer = 1 To matchResults.Groups.Count - 1
Dim groupObj As Group = matchResults.Groups(i)
' matched text: groupObj.Value
' match start: groupObj.Index
' match length: groupObj.Length
If groupObj.Success Then
End If
Next
End While
精彩评论