一.代码如下:
package train;
import java.util.regex.*;
public class Test {
public static void main(String[] args) {
Pattern p;//定义一个模式对象
Matcher m;//定义一个匹配对象
String regex = "[1-9][0-9]*[.]?[0-9]*";
p = Pattern.compile(regex);//初始化模式对象
String s = "Price:123.456$,weight:234.78,height:83";
m = p.matcher(s);//初始化匹配对象
while (m.find()) {
String str = m.group();
System.out.println("从" + m.start() + "到" + m.end() + "匹配模式子序列:");
System.out.println(str);
}
}
}
二.运行结果如下: