正则表达式(二)
public static void main(String[] args) {
String ss = ",一@CNUM,一斤@CQUAN,斤@COUNT,1点1@CNUM,1点1斤@CQUAN,斤@COUNT,西安@CN_ADJ,优质@CN_ADJ,大白菜@CN_WORD,";
Matcher m = Pattern.compile(",.*?@CQUAN").matcher(ss);
while(m.find()){
System.out.println(m.group());
}
}
输出:
,一@CNUM,一斤@CQUAN
,斤@COUNT,1点1@CNUM,1点1斤@CQUAN
一个明确的字符,从最左边第一个匹配到的字符开始, 贪婪和懒惰匹配针对的是数量词字符,如
* 或 + 或 ? 或 {n} 或 {n,} 或 {n,m}
public static void main(String[] args) {
String ss = ",一@CNUM,一斤@CQUAN,斤@COUNT,1点1@CNUM,1点1斤@CQUAN,斤@COUNT,西安@CN_ADJ,优质@CN_ADJ,大白菜@CN_WORD,";
Matcher m = Pattern.compile(",[^,]*?@CQUAN").matcher(ss);
while(m.find()){
System.out.println(m.group());
}
}
输出
,一斤@CQUAN
,1点1斤@CQUAN
再举个例子
public static void main(String[] args) {
String ss = "232456778";
System.out.println(ss.replaceAll("2.*7",""));
System.out.println(ss.replaceAll("2.*?7",""));
}
输出
8
78