Reputation: 307
The following is my java code
Pattern p = Pattern.compile("-?\\d*\\.?\\d*");
Matcher m = p.matcher("the numbers are -3.4 and 132");
while (m.find()) {
System.out.println(m.group());
}
But it fails to match either number. Can anyone shed some light upon this program?
Upvotes: 0
Views: 69
Reputation: 48817
Your regex matches the numbers, but also every inter-char. Use \\d+
instead of your second \\d*
for example.
I usually use the following regex to match numbers (already escaped for Java):
[-+]?\\d*[.]?\\d+(?:[eE][-+]?\\d+)?
Upvotes: 4