直接上代码:
1 @Test
2
3 public void contextLoads() {
4
5 /*String str="this is (Tom) and \"Eric\", this is \"Bruce lee\", he is a chinese, name is \"李小龙\"。";
6
7 Pattern p= Pattern.compile("\"(.*?)\"");
8
9 Matcher m=p.matcher(str);
10
11 while(m.find()){
12
13 System.out.println(m.group());
14
15 }*/
16
17
18
19
20
21 /*String str="this is [Tom] and , he is a [chinese], name [is]。";
22
23 Matcher mat = Pattern.compile("(?<=\\[)(\\S+)(?=\\])").matcher(str);
24
25 while(mat.find()){
26
27 System.out.println(mat.group());
28
29 }*/
30
31
32
33
34
35 String filetext = "//[张小名] 25分//[李小花] 43分//[王力] 100分";
36
37 Pattern p = Pattern.compile("\\[(.*?)\\]");//正则表达式,取=和|之间的字符串,不包括=和|
38
39 Matcher m = p.matcher(filetext);
40
41 while(m.find()) {
42
43 System.out.println(m.group(0));//m.group(1)不包括这两个字符
44
45 }
46
47 }
来源:https://www.cnblogs.com/smartisn/p/12276032.html