Search string for specific numbers

I am trying to find a quick solution for finding parts in a string. Here is an example line:

"PostLoad is successful! You have transferred the amount of 17.00 Rs to 03334224222. Now use PostLoad by typing 123. PostLoad by SMS will end 01-03-2011."

Purpose: you need to get bold values: the number and cell number. The contents of the line change slightly, but the cell number will always be 11 digits. The sum is always with two decimal values. Any suggestions using C # and RegEx?

+3
source share
2 answers
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 a decimal number, group # 2 will contain an 11-digit number.

A " " - - -- , .

, , 12.3456, ; , , , . , number12.34 12.34.

+10

VB.Net. , .

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
0

Source: https://habr.com/ru/post/1785368/


All Articles