Reputation: 111
I have response text:
Cuoc no truoc -2.134VND. Cuoc phat sinh tam tinh den 31/08/2018:
3`2.666VND (da tru KM,goi cuoc...). TKFastpay: 0VND.Tra 01.Trang sau 02.Thoat\",15
i want get result is value of money before "VND" -> -2.134
and 32.666
and 0
.
I have regex
String regex = "(?<![^=])([\\d]*)(?!$[VND])";
but its not work. Please help me!
Upvotes: 0
Views: 1070
Reputation: 196
Try this simple regex
In Java
String regex = "(-?\\d*?\\.?\\d+)(?=VND)";
regex
(-?\d*?\.?\d+)(?=VND)
see regex sample
Upvotes: 0
Reputation: 163457
You could use a positive lookahead (?=
and a word boundary after VND \b
.
-?\d+(?:\.\d+)?(?=VND\b)
That would match
-?
Optional minus sign (To also allow a plus, you could use an optional character class [+-]?
\d+
Match one or more digits(?:\.\d+)?
An optional non capturing group matching a dot and one or more digits(?=VND\b)
Positive lookahead that asserts what is on the right is VND
In Java:
-?\\d+(?:\\.\\d+)?(?=VND\\b)
Upvotes: 3
Reputation: 785481
You may use this regex with a lookahead:
[+-]?\d+\.?\d*(?=VND)
RegEx Details:
[+-]?
: Match optional +
or -
\d+\.?\d*
: Match a floating point number or integer number(?=VND)
: Assert that we have VND
at next positionJava Code:
final String regex = "[+-]?\\d+\\.?\\d*(?=VND)";
final String string = "Cuoc no truoc -2.134VND. Cuoc phat sinh tam tinh den 31/08/2018:\n"
+ "32.666VND (da tru KM,goi cuoc...). TKFastpay: 0VND.Tra 01.Trang sau 02.Thoat\\\",15";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println(matcher.group(0));
}
Upvotes: 2
Reputation: 4403
If I understand the requirement, then the regex, used in a repeated fashion, might be:
(-?)[\\d][\\d.]*(?=VND)
The idea being that you need at least one digit, followed by more digits or a decimal, then followed by VND.
A slightly improved approach would be to split the [.] to be between the digits, so: ((-?)[\d]+[.]?[\d]*)(?=VND)
Upvotes: 1
Reputation: 2230
String regex = "(-?[0-9]+[\.]+[0-9]*)VND"
Upvotes: 1