首页 > 代码库 > Java 正则表达式 向前、向后匹配

Java 正则表达式 向前、向后匹配

//向后匹配

String a = "I paid $90 for 10 oranges, 12 pears and 8 apples. I saved $5 on ";
Pattern p = Pattern.compile("(?<=\\$)\\d+");
Matcher m = p.matcher(a);
while (m.find ())
{
String group = m.group ();
System.out.println (group);
}

向前匹配
a = "https://mail.huawei.com ";
p = Pattern.compile(".+(?=:)");
m = p.matcher(a);
while (m.find ())
{
String group = m.group ();
System.out.println (group);
}