Java正则表达式

上一篇我们介绍了正则表达式,本文我们细数Java中的正则表达式应用,主要介绍在String中和Pattern与Matcher的用法

String中的正则表达式

matches

public boolean matches(String regex)
匹配某正则表达式:返回boolean类型值

例如:

1
2
3
4
5
6
//一段文字
String text =
"[2018-01-20] this is a text,,,and hello world,hello everyone,";
boolean bool = text.matches("^\\[20\\d\\d-\\d\\d-\\d\\d\\].*");

//true

replaceFirst

public String replaceFirst(String regex, String replacement)
替换第一个符合正则的值 替换为replacement

例如:替换第一个llo结尾的单词

1
2
3
4
5
6
//一段文字
String text =
"[2018-01-20] this is a text,,,and hello world,hello everyone,";

String result = text.replaceFirst("\\w+llo","world");
//[2018-01-20] this is a text,,,and world world,hello everyone,

replaceAll

public String replaceAll(String regex, String replacement)
替换第所有符合正则的值 替换为replacement

1
2
3
4
5
6
7
//一段文字
String text =
"[2018-01-20] this is a text,,,and hello world,hello everyone,";

result = text.replaceAll("\\w+llo", "world");

//[2018-01-20] this is a text,,,and world world,world everyone,

split

public String[] split(String regex)

根据给定正则表达式的匹配拆分此字符串,它是全部拆分,最后一个值如果是被拆分的字符串,则不计入,第一个如果是被拆分的字符串,则记为一个""空字符串,中间的如果是几个被分隔的字符串,则记为""空字符串

例如:按照,拆分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
String text = 
",[2018-01-20] this is a text,,,and hello world,hello everyone,";
String[] arr = text.split(",", 2);
System.out.println(arr.length);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}


/**
6
[2018-01-20] this is a text


and hello world
hello everyone
**/

public String[] split(String regex, int limit)

根据匹配给定的正则表达式来拆分此字符串,limit 是限制拆分的次数,实际分解的次数是 limit-1 次,limit 就是分解后数组的 length。

1
2
3
4
5
6
7
8
9
10
11
12
String text =
",[2018-01-20] this is a text,,,and hello world,hello everyone,";
String[] arr = text.split(",", 2);
System.out.println(arr.length);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
/**
2

[2018-01-20] this is a text,,,and hello world,hello everyone,
**/

注意replace方法

public String replace(CharSequence target, CharSequence replacement)
target的值是要替换的值,而不是正则表达式,所以与上面有区别.
所有target会被替换为replacement

例如:替换text中的hello

1
2
3
4
5
6
7
//一段文字
String text =
"[2018-01-20] this is a text,,,and hello world,hello everyone,";

result = text.replace("hello", "world");
System.out.println("replace() "+ result);
//[2018-01-20] this is a text,,,and world world,world everyone,

Pattern 和 Matcher

Pattern

Matcher

Pattern 和 Matcher例子

需求将文件夹里面的数据转为json串样式:
形如:

文件夹里数据:
00望远镜(形)

结果数据:
{id:00, title:"00 望远镜", desc:"形", cardUrl:"https://www.iteway.com/uploads/super_memory/00.jpg"}

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author zhaomingwei
* Created on 18/1/17.
*/
public class FormatStr {
private static final Pattern PATTERN =
Pattern.compile("(\\d+)([\\u4e00-\\u9fa5]+)(()([\\u4e00-\\u9fa5]+)())");
public static void main(String[] args) throws IOException {
BufferedReader reader =
new BufferedReader(new FileReader(new File("/Users/zhaomingwei/Downloads/number")));
TreeMap<Integer,NumberEncode> treeMap = new TreeMap();
String line;

while ((line=reader.readLine()) !=null){
//System.out.println(line);
Matcher matcher = PATTERN.matcher(line);
while (matcher.find()){
treeMap.put(Integer.parseInt(matcher.group(1)),
new NumberEncode(matcher.group(1),
matcher.group(1)+" "+matcher.group(2),
matcher.group(4),
"https://www.iteway.com/uploads/super_memory/"+
matcher.group(1)+".jpg"
));
}
}
for (Map.Entry<Integer,NumberEncode> entry: treeMap.entrySet()){
System.out.print(entry.getValue());
System.out.println(",");
}
}
}

class NumberEncode{
private String id;
private String title;
private String desc;
private String cardUrl;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getDesc() {
return desc;
}

public void setDesc(String desc) {
this.desc = desc;
}

public String getCardUrl() {
return cardUrl;
}

public void setCardUrl(String cardUrl) {
this.cardUrl = cardUrl;
}

public NumberEncode(String id, String title, String desc, String cardUrl) {
this.id = id;
this.title = title;
this.desc = desc;
this.cardUrl = cardUrl;
}

@Override
public String toString() {
return "{" +
"id:" + id +
", title:\"" + title + '\"' +
", desc:\"" + desc + '\"' +
", cardUrl:\"" + cardUrl + '\"' +
'}';
}
}
#点击此处展开文件内容

-------------感谢您的阅读祝您生活愉快!-------------

本文标题:Java正则表达式

文章作者:小明

发布时间:2018年01月30日 - 17:01

最后更新:2018年01月30日 - 17:01

原始链接:https://www.iteway.com/2018/01/30/Java正则表达式/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

支持一杯咖啡
0%