首页 > 编程语言 > 详细

Java 正则表达式 regex 提取字符

时间:2020-02-08 15:04:30      阅读:60      评论:0      收藏:0      [点我收藏+]

直接上代码:

 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     }

 

Java 正则表达式 regex 提取字符

原文:https://www.cnblogs.com/smartisn/p/12276032.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!