Java 中 的 Regular (一)

1.圆括号()是组,主要应用在限制多选结构的范围/分组/捕获文本/环视/特殊模式处理。
说白了就是:只是把括号内的内容作为同一个表达式来处理,例如(ab){1,3},就表示ab一起连续出现最少1次,最多3次。
再比如:(abc|bcd|cde),表示这一段是abc、bcd、cde三者之一均可,顺序也必须一致。
      String content="国药控股有国大药房新疆新特药业连锁有限责任公司乌鲁木齐市西站三分店";
if(content.lastIndexOf("店")!=-1&&content.contains("公司")){
String regRegular="(股份|股有|有限|服务)([a-zA-Z0-9_\u4e00-\u9fa5])*公司";//(?!控股|股有)
//String regRegular="Windows(?!95|98|NT|2000)xp";
Pattern p=Pattern.compile(regRegular);
 Matcher m=p.matcher(content);
 int count=0;
 if(m.find()){
 String s=m.group(0);
 System.out.println(m.start()+"==="+ m.end());
 System.out.println("reg:"+s);
 System.out.println(content);
 content=content.replace(s, "");
 
 }
}

//结果为:股有国大药房新疆新特药业连锁有限责任公司

1.1 例子

public class Test {
public static void main(String[] args) {
String content="中国新华航空集团有限公司、b公司以及c公司互保金额为900";//中国新华航空集团有限公司与c公司互保金额为900
Pattern p=Pattern.compile("(与|以及).*互保");//与,以及分别作为单独的词去匹配。
Matcher m=p.matcher(content);
  if(m.find()){
    System.out.println(m.group(0));
   }
}
}

//以及c公司互保

2.方括号是单个字符匹配,字符集/排除字符集/命名字符集。说白就是:表示匹配的字符在[]中,并且只能出现一次,并且特殊字符写在[]会被当成普通字符来匹配
  2.1 String content="国药控le股有国大药房新疆新特药业连锁有限责任公司乌鲁木齐市西站三分店";
if(content.lastIndexOf("店")!=-1&&content.contains("公司")){
String regRegular="[股份|控股|有限|服务]([a-zA-Z0-9_\u4e00-\u9fa5])*公司";
//String regRegular="Windows(?!95|98|NT|2000)xp";
Pattern p=Pattern.compile(regRegular);
 Matcher m=p.matcher(content);
 int count=0;
 if(m.find()){
 String s=m.group(0);
 System.out.println(m.start()+"==="+ m.end());
 System.out.println("reg:"+s);
 System.out.println(content);
 content=content.replace(s, "");
 
 }
}
}
//结果:控le股有国大药房新疆新特药业连锁有限责任公司
从结果分析:[]为单个字符匹配,虽然里面配置有股份,控股,有限,服务等词,它是按照单字匹配的原则,内容中“控le股”中的“控”和正则中“控股”中“控”单字匹配上,并不是正则中控股这个词去匹配的。


  2.2 加上匹配次数
      String content="国药控le股有国大药房新疆新特药业连锁有限责任公司乌鲁木齐市西站三分店";
if(content.lastIndexOf("店")!=-1&&content.contains("公司")){
String regRegular="[股份|控股|有限|服务]{2}([a-zA-Z0-9_\u4e00-\u9fa5])*公司";//加上匹配次数
//String regRegular="Windows(?!95|98|NT|2000)xp";
Pattern p=Pattern.compile(regRegular);
 Matcher m=p.matcher(content);
 int count=0;
 if(m.find()){
 String s=m.group(0);
 System.out.println(m.start()+"==="+ m.end());
 System.out.println("reg:"+s);
 System.out.println(content);
 content=content.replace(s, "");
 
 }
}


//结果:股有国大药房新疆新特药业连锁有限责任公司
//[]为单个字符匹配,加了匹配次数,恰好两次,正则中“控股”中的“股”和“有限”中的“有”两个单个字符,组合成‘股有’和内容中发生了匹配,


2.3  采用过滤排除掉,一些不要的词汇:
  String content="国药控股有国大药房新疆新特药业连锁有限责任公司乌鲁木齐市西站三分店";
if(content.lastIndexOf("店")!=-1&&content.contains("公司")){
String regRegular="(?!控股|股有)[股份|控股|有限|服务]{2}([a-zA-Z0-9_\u4e00-\u9fa5])*公司";
//String regRegular="Windows(?!95|98|NT|2000)xp";
Pattern p=Pattern.compile(regRegular);
 Matcher m=p.matcher(content);
 int count=0;
 if(m.find()){
 String s=m.group(0);
 System.out.println(m.start()+"==="+ m.end());
 System.out.println("reg:"+s);
 System.out.println(content);
 content=content.replace(s, "");
 
 }
}
//结果:有限责任公司;(?!控股|股有);排除掉“控股”和“股有”这两个词汇。
if(content.lastIndexOf("店")!=-1&&content.contains("公司")){
//String regRegular="(?!控股|股有)[股份|控股|有限|服务]{2}([a-zA-Z0-9_\u4e00-\u9fa5])*公司";
String regRegular="Windows(?!95|98|NT|2000)xp";
Pattern p=Pattern.compile(regRegular);
 Matcher m=p.matcher("Windowsxp");
 int count=0;
 if(m.find()){
 String s=m.group(0);
 System.out.println(m.start()+"==="+ m.end());
 System.out.println("reg:"+s);
 System.out.println(content);
 content=content.replace(s, "");
 
 }
}
//Windowsxp;排除windows 95,98,nt,2000
http://blog.csdn.net/lyd135364/article/details/53157416
http://www.dewen.net.cn/q/9109/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E4%B8%AD()%E5%92%8C[]%E6%9C%89%E4%BB%80%E4%B9%88%E5%8C%BA%E5%88%AB%EF%BC%9F




3.贪婪匹配:在满足匹配时,匹配尽可能长的字符串,默认情况下,采用贪婪匹配
String regRegular="a.*c";
Pattern p=Pattern.compile(regRegular);
 Matcher m=p.matcher("qsdf@163.ascdeacfabc.com");
 int count=0;
 if(m.find()){
 String s=m.group(0);
 System.out.println(m.start()+"==="+ m.end());
 System.out.println("reg:"+s);
 System.out.println(content);
 content=content.replace(s, "");
 
 }
//结果:ascdeacfabc.c
4.非贪婪模式:在满足匹配时,匹配尽可能短的字符串,使用?来表示非贪婪匹配
String regRegular="a.*?c";
Pattern p=Pattern.compile(regRegular);
 Matcher m=p.matcher("qsdf@163.ascdeacfabc.com");
 int count=0;
 if(m.find()){
 String s=m.group(0);
 System.out.println(m.start()+"==="+ m.end());
 System.out.println("reg:"+s);
 System.out.println(content);
 content=content.replace(s, "");
 
 }
}
//asc
https://segmentfault.com/q/1010000000166874

以下是常用的限定符代码:
*重复零次或更多次
+重复一次或更多次
?重复零次或一次
{n}重复n次
{n,}重复n次或更多次
{n,m}重复n到m次
.点 匹配除“\r\n”之外的任何单个字符。要匹配包括“\r\n”在内的任何字符,请使用像“[\s\S]”的模式。
.* 可以匹配任意长度的字符。尽量去匹配尽可能长的字符串 。是贪婪模式

.* ?满足匹配后,尽量去匹配尽可能短的字符串,是非贪婪模式。

具体文件:见百度网盘


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值