一个可以链式使用正则的类

最近使用webMgaic爬虫框架的时候,发现链式调用真的很好用,能省掉很多套路性的代码,所以也自己动手写了一个可以链式调用进行正则筛选的工具类。下面上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.regex.*;
/**
* Created by wht on 2017/3/18.
*/
public class RexString {
private String string = "";
public RexString() {
}
public RexString(String string) {
this.string = string;
}
public RexString getRex(String rex) {
Pattern p = Pattern.compile(rex);
Matcher m = p.matcher(string);
if (m.find()) {
return new RexString(m.group());
}
return new RexString();
}
@Override
public String toString() {
return this.string;
}
}

使用:

1
2
3
RexString rexString = new RexString("xxx");
RexString rs = rexString.getRex(rex1).getRex(rex2).getRex(rex3) ···
String result = rs.toString();


本文章首发www.whtis.com,转载请注明出处


如果觉得这篇文章还有用的话,请我喝杯饮料呗~~