Reputation: 291
I want to get the text after a string, that string is compound by a string and a number. I have the following string:
"Some text before and Total payment: 13214 EUR Signature:"
I want to get the "EUR", but only the EUR that is after "Total payment: xxxxx", because I can have "xxxxx EUR" in my string somewhere else.
I have tried
(?!<Total payment )(\d+) ([A-Z]){3,}
it will get what I want but from all places where appears. I have looked at other questions but it is not what I want.
Upvotes: 2
Views: 3332
Reputation: 626689
You may capture the part of the string you want into a capturing group while matching the left-hand side without capturing it:
Dim my_rx As Regex = New Regex("Total payment:\s*\d+\s*([A-Z]{3,})")
Dim my_result As Match = my_rx.Match("Some text before and Total payment: 13214 EUR Signature:")
If my_result.Success Then
Console.WriteLine(my_result.Groups(1).Value) ' => EUR
End If
See the VB.NET demo online.
Details
Total payment:
- a literal substring (just matched to ensure correct context)\s*
- 0+ whitespace chars\d+
- 1+ digits\s*
- 0+ whitespace chars([A-Z]{3,})
- Capturing group 1 (m.Groups(1).Value
): 3 or more uppercase ASCII letters.Note you may also use a named capturing groups: Total payment:\s*\d+\s*(?<currency>[A-Z]{3,})
and then access the value with m.Groups("currency").Value
.
See the regex demo here.
Upvotes: 2
Reputation: 189
its easy you need to make your regex non-greedy. this should work
Total payment.*?EUR
adding a ? after .* forces it to only match the first match of EUR. it will also match the literal string before .? and .? matches everything between the literal string and the first instance of EUR.
Upvotes: 0