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?
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+)?
使用此正则表达式:
-?\\d+(?:\\.\\d+)?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.