Java使用正则表达式
2016-02-04 10:58
441 查看
Java对正则表达式的支持并不是很好,知道JDK1.8都不能支持正则表达式中的平衡组,只能支持(?<name>exp)这样的捕获方式
平衡组中用到的表达式如下:
(?'name'子表达式A) ,若成功匹配子表达式A,则往名为name的栈空间压一个元素。
(?'-name'子表达式A) ,若成功匹配子表达式A,则弹出名为name的栈空间的栈顶元素,弹出元素后若栈空间为空则结束匹配。
(?(name)yes表达式|no表达式) ,若名为name的栈空间非空,则使用yes表达式进行匹配,否则则使用no表达式进行匹配。
(?(name)yes表达式) ,若名为name的栈空间非空,则使用yes表达式进行匹配。
(?!) ,由于没有后缀表达式,因此总会导致匹配失败并结束匹配。
java中使用正则表达式:
官网的使用解释:
A typical invocation sequence is thus
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
boolean b = m.matches();
A matches method is defined by this class as a convenience for when a regular expression is used just once. This method compiles an expression and matches an input sequence against it in a single invocation. The statement
boolean b = Pattern.matches("a*b", "aaaaab");
find() Attempts to find the next subsequence of the input sequence that matches the pattern.
group(int group) Returns the input subsequence matched by the previous match.
Parameters:group - The index of a capturing group in this matcher's pattern
下面来一个实际的例子:
平衡组中用到的表达式如下:
(?'name'子表达式A) ,若成功匹配子表达式A,则往名为name的栈空间压一个元素。
(?'-name'子表达式A) ,若成功匹配子表达式A,则弹出名为name的栈空间的栈顶元素,弹出元素后若栈空间为空则结束匹配。
(?(name)yes表达式|no表达式) ,若名为name的栈空间非空,则使用yes表达式进行匹配,否则则使用no表达式进行匹配。
(?(name)yes表达式) ,若名为name的栈空间非空,则使用yes表达式进行匹配。
(?!) ,由于没有后缀表达式,因此总会导致匹配失败并结束匹配。
java中使用正则表达式:
官网的使用解释:
A typical invocation sequence is thus
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
boolean b = m.matches();
A matches method is defined by this class as a convenience for when a regular expression is used just once. This method compiles an expression and matches an input sequence against it in a single invocation. The statement
boolean b = Pattern.matches("a*b", "aaaaab");
find() Attempts to find the next subsequence of the input sequence that matches the pattern.
group(int group) Returns the input subsequence matched by the previous match.
Parameters:group - The index of a capturing group in this matcher's pattern
下面来一个实际的例子:
String str = "ds=222&de=wew"; String regex = "(^|&)ds=([^&]*)(&|$)"; Pattern pattern = Pattern.compile(regex); Matcher match = pattern.matcher(str); System.out.println(match.find()); \\打印 true,必须增加该条代码,否则下面的无法捕获 System.out.println(match.group(2)); \\打印 222</span>
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- msql 正则表达式
- 正则表达式
- 插入排序
- 冒泡排序
- 堆排序