`
gaozzsoft
  • 浏览: 414566 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

Java正则表达式

阅读更多
关键字: java 正则表达式 regular
Java代码
1.public class RegularTest {  
2.    public static void main(String[] args){  
3.        p("str".matches("..."));  
4.        //public String replaceAll(String regex, String replacement)  
5.        //public String replaceFirst(String regex, String replacement)  
6.        p("A00123lB".replaceFirst("\\d","*"));  
7.        p("A00123lB".replaceAll("\\d", "*"));  
8.//      []表示范围,{}表示长度  
9.//      [0-9]表示在0到9之前的一个数  
10.//      \\d{0,10}表示0到10之间的长度  
11.//      Greedy 数量词  
12.//      X?      X,一次或一次也没有   
13.//      X*      X,零次或多次   
14.//      X+      X,一次或多次   
15.//      X{n}    X,恰好 n 次   
16.//      X{n,}   X,至少 n 次   
17.//      X{n,m}  X,至少 n 次,但是不超过 m 次  
18.//      "\\" 匹配 反斜线字符(\)  
19.//      "\\".matches("\\\\")  一个\匹配的时候要写四个  
20.//      [abc] a、b 或 c(简单类)   
21.//      [^abc] 任何字符,除了 a、b 或 c(否定)   
22.//      [a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围)(并集[a-z]|[A-Z])   
23.//      [a-d[m-p]] a 到 d 或 m 到 p:[a-dm-p](并集)   
24.//      [a-z&&[def]] d、e 或 f(交集)   
25.//      [a-z&&[^bc]] a 到 z,除了 b 和 c:[ad-z](减去)   
26.//      [a-z&&[^m-p]] a 到 z,而非 m 到 p:[a-lq-z](减去)   
27.          
28.//      . 任何字符(与行结束符可能匹配也可能不匹配)   
29.//      \d 数字:[0-9]   
30.//      \D 非数字: [^0-9]   
31.//      \s 空白字符:[ \t\n\x0B\f\r]   
32.//      \S 非空白字符:[^\s]   
33.//      \w 单词字符:[a-zA-Z_0-9]   
34.//      \W 非单词字符:[^\w]   
35.//      ^ 行的开头   
36.//      $ 行的结尾   
37.//      \b 单词边界   
38.//      \B 非单词边界   
39.//      p(" \n".matches("^[\\s&&[^\\n]]*\\n$"));这个匹配空白字付(开始是空白,但不是换行,结果是换行)  
40.//      p("hello sir".matches("^h.*r$"));  
41.//      "lan ".matches(".*\\z$") 这样的不能单词边界,要用行的结尾\z  
42.          
43.//      Reluctant 数量词(这个是勉强的,先用最少的去匹配)   
44.//      X?? X,一次或一次也没有   
45.//      X*? X,零次或多次   
46.//      X+? X,一次或多次   
47.//      X{n}? X,恰好 n 次   
48.//      X{n,}? X,至少 n 次   
49.//      X{n,m}? X,至少 n 次,但是不超过 m 次   
50.          
51.//      Possessive 数量词 (这个用来追求效率,直接打最多的,不行的话就不行了)  
52.//      X?+ X,一次或一次也没有   
53.//      X*+ X,零次或多次   
54.//      X++ X,一次或多次   
55.//      X{n}+ X,恰好 n 次   
56.//      X{n,}+ X,至少 n 次   
57.//      X{n,m}+ X,至少 n 次,但是不超过 m 次   
58.//      文字替换(首次出现字符)  
59.//      Pattern pattern = Pattern.compile("正则表达式");  
60.//      Matcher matcher = pattern.matcher("正则表达式 Hello World,正则表达式 Hello World");  
61.//      替换第一个符合正则的数据  
62.//      System.out.println(matcher.replaceFirst("Java"));  
63.//  
64.//      文字替换(全部)  
65.//      Pattern pattern = Pattern.compile("正则表达式");  
66.//      Matcher matcher = pattern.matcher("正则表达式 Hello World,正则表达式 Hello World");  
67.//      替换第一个符合正则的数据  
68.//      System.out.println(matcher.replaceAll("Java"));  
69.//      去除html标记  
70.//      Pattern pattern = Pattern.compile("<.+?>", Pattern.DOTALL);  
71.//      Matcher matcher = pattern.matcher("<a href=\"index.html\">主页</a>");  
72.//      String string = matcher.replaceAll("");  
73.//      System.out.println(string);  
74.          
75.//      截取http://地址  
76.//      截取url  
77.//      Pattern pattern = Pattern.compile("(http://|https://){1}[\\w\\.\\-/:]+");  
78.//      Matcher matcher = pattern.matcher("dsdsds<http://dsds//gfgffdfd>fdf");  
79.//      StringBuffer buffer = new StringBuffer();  
80.//      while(matcher.find()){                
81.//          buffer.append(matcher.group());          
82.//          buffer.append("\r\n");                
83.//      System.out.println(buffer.toString());  
84.//      }  
85.//                 匹配中文的Regular [\\u4e00-\\u9fa5]  
86.          
87.        //电子邮件的匹配  
88.        Pattern email = Pattern.compile("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+");  
89.        Matcher emailMatcher = email.matcher("langhu@qq.com");  
90.        //Matcher是匹配整个字符串  
91.        //find()找子串,start()找到子串开始的位置,end()找到子串结束的位置,  
92.        //但必须能找到子串才能用start,end这两个方法reset()重置          
93.        p(emailMatcher.matches());  
94.          
95.        //appendReplacement()和appendTail()方法  
96.        //忽略大小写Pattern.CASE_INSENSITIVE  
97.        String str = "langhua lanHUA LANghua lANGHUa dsfd LANGHUA T";  
98.        Pattern langhuaParttern = Pattern.compile("langhua", Pattern.CASE_INSENSITIVE);  
99.        Matcher langhuaMatcher = langhuaParttern.matcher(str);  
100.        StringBuffer sb = new StringBuffer();  
101.        int i = 0;  
102.        while(langhuaMatcher.find()){  
103.            i++;  
104.            if(i%2==0){  
105.                langhuaMatcher.appendReplacement(sb,"xiaolanghua");  
106.            }else{  
107.                langhuaMatcher.appendReplacement(sb,"dalanghua");  
108.            }             
109.        }  
110.        langhuaMatcher.appendTail(sb);  
111.        p(sb.toString());  
112.        //分组group()返回的是子串  
113.        String num = "123aa-1234aa-12384bb-0-*-*";  
114.        Pattern nump = Pattern.compile("(\\d{3,5})(\\w{2})");  
115.        Matcher numm = nump.matcher(num);  
116.        while(numm.find()){  
117.            p(numm.group(1));  
118.        }  
119.          
120.        Pattern pattern = Pattern.compile(".{1,3}?\\d");  
121.        Matcher marcher = pattern.matcher("2地3基13ab2cef2");  
122.        if(marcher.find()){  
123.            p(marcher.group());  
124.            p(marcher.start()+"-"+marcher.end());  
125.        }  
126.    }  
127.    public static void p(Object o){  
128.        System.out.println(o);  
129.    }  
130.} 

=============================================================
string.replaceAll()中的特殊字符($ \)与matcher.appendReplacement
string.replaceAll中的特殊字符
string.replaceAll(String regex, String replacement)中的replacement参数即替换内容中含有特殊字符 $ \ 时,需转义。



Java代码
1./* 
2. * 字符串"$ \"中的$与\字符互换位置 
3. */ 
4.public class SpecialCharReplace {  
5.    public static void main(String[] args) {  
6.        String str = "$ \\";  
7.        /* 
8.         * string.replaceAll()中的特殊字符 $ 与 \  
9.         *  
10.         * 由于 $ 字符在作为替换内容时,是一个特殊字符,指反向引用前面的分组内容,所以把 
11.         * 某字符替换成 $ 字符时,因该在前面加上转义字符 \。 
12.         * \ 字符就不用说了,本身就是转义字符,但为什么在作为替换内容时要使用四个 \ 字符 
13.         * ,这里又不是用在正则表达式里?这就是因为 \ 字符在作为替换内容里也是一个特殊字 
14.         * 符,它用来将前面讲的 $ 字符进行转换的,所以也为特殊字符。以下是replaceAll的 
15.         * 源码片断,从源码就可以看出 \$ 是两个特殊字符 
16.         *  
17.         * if (nextChar == '\\') { 
18.         *      cursor++; 
19.         *      nextChar = replacement.charAt(cursor); 
20.         *      result.append(nextChar); 
21.         *      cursor++; 
22.         * } else if (nextChar == '$') { 
23.         *      // Skip past $ 
24.         *      cursor++; 
25.         *      ... 
26.         * }else { 
27.         *      result.append(nextChar); 
28.         *      cursor++; 
29.         * } 
30.         */ 
31.        System.out.println(str.replaceAll("\\$(\\W)\\\\", "\\\\$1\\$"));// \ $  
32.    }  
33. 
34.}  
/*
* 字符串"$ \"中的$与\字符互换位置
*/
public class SpecialCharReplace {
public static void main(String[] args) {
String str = "$ \\";
/*
* string.replaceAll()中的特殊字符 $ 与 \
*
* 由于 $ 字符在作为替换内容时,是一个特殊字符,指反向引用前面的分组内容,所以把
* 某字符替换成 $ 字符时,因该在前面加上转义字符 \。
* \ 字符就不用说了,本身就是转义字符,但为什么在作为替换内容时要使用四个 \ 字符
* ,这里又不是用在正则表达式里?这就是因为 \ 字符在作为替换内容里也是一个特殊字
* 符,它用来将前面讲的 $ 字符进行转换的,所以也为特殊字符。以下是replaceAll的
* 源码片断,从源码就可以看出 \$ 是两个特殊字符
*
* if (nextChar == '\\') {
*      cursor++;
*      nextChar = replacement.charAt(cursor);
*      result.append(nextChar);
*      cursor++;
* } else if (nextChar == '$') {
*      // Skip past $
*      cursor++;
*      ...
* }else {
*      result.append(nextChar);
*      cursor++;
* }
*/
System.out.println(str.replaceAll("\\$(\\W)\\\\", "\\\\$1\\$"));// \ $
}

} Matcher对象的appendReplacement典型应用与特殊字符&\的进一步分析

问题的提出


字符串模板:
    String template="尊敬的客户${customerName}你好!本次消费金额${amount},您帐户${accountNumber}上的余额为${balance},欢迎下次光临!";
其中以 ${ 开始 } 结尾的为待替换的变量域。
数据存放于Map中,key为域名,value为域值。如:
Map--
    customerName = 刘明
    accountNumber = 888888888
    balance = $1000000.00
    amount = $1000.00
请编写函数:
    public static String composeMessage(String template, Map data) throw Exception
实现将任意模板字符串中的变量域,按域名替换为data中的域值。
例如,上例替换结果为:
    "尊敬的客户刘明你好!本次消费金额$1000.00,您帐户888888888上的余额为$1000000.00,欢迎下次光临!"
注:如果Map中找不到域值,以空字符串""替换。

问题的解决

Java代码
1.public class RegexExam {  
2.    public static void main(String args[]) {  
3.        HashMap data = new HashMap();  
4.        String template = "尊敬的客户${customerName}你好!本次消费金额${amount}," 
5.                + "您帐户${accountNumber}上的余额为${balance},欢迎下次光临!";  
6.        data.put("customerName", "刘明");  
7.        data.put("accountNumber", "888888888");  
8.        data.put("balance", "$1000000.00");  
9.        data.put("amount", "$1000.00");  
10.        try {  
11.            System.out.println(composeMessage(template, data));  
12.        } catch (Exception e) {  
13.            e.printStackTrace();  
14.        }  
15.    }  
16. 
17.    public static String composeMessage(String template, Map data)  
18.            throws Exception {  
19.        String regex = "\\$\\{(.+?)\\}";  
20.        Pattern pattern = Pattern.compile(regex);  
21.        Matcher matcher = pattern.matcher(template);  
22.        /* 
23.         * sb用来存储替换过的内容,它会把多次处理过的字符串按源字符串序 
24.         * 存储起来。 
25.         */ 
26.        StringBuffer sb = new StringBuffer();  
27.        while (matcher.find()) {  
28.            String name = matcher.group(1);//键名  
29.            String value = (String) data.get(name);//键值  
30.            if (value == null) {  
31.                value = "";  
32.            } else {  
33.                /* 
34.                 * 由于$出现在replacement中时,表示对捕获组的反向引用,所以要对上面替换内容 
35.                 * 中的 $ 进行替换,让它们变成 "\$1000.00" 或 "\$1000000000.00" ,这样 
36.                 * 在下面使用 matcher.appendReplacement(sb, value) 进行替换时就不会把 
37.                 * $1 看成是对组的反向引用了,否则会使用子匹配项值amount 或 balance替换 $1 
38.                 * ,最后会得到错误结果: 
39.                 * 
40.                 * 尊敬的客户刘明你好!本次消费金额amount000.00,您帐户888888888上的余额 
41.                 * 为balance000000.00,欢迎下次光临! 
42.                 * 
43.                 * 要把 $ 替换成 \$ ,则要使用 \\\\\\& 来替换,因为一个 \ 要使用 \\\ 来进 
44.                 * 行替换,而一个 $ 要使用 \\$ 来进行替换,因 \ 与  $ 在作为替换内容时都属于 
45.                 * 特殊字符:$ 字符表示反向引用组,而 \ 字符又是用来转义 $ 字符的。 
46.                 */ 
47.                value = value.replaceAll("\\$", "\\\\\\$");  
48.                //System.out.println("value=" + value);  
49.            }  
50.            /* 
51.             * 经过上面的替换操作,现在的 value 中含有 $ 特殊字符的内容被换成了"\$1000.00" 
52.             * 或 "\$1000000000.00" 了,最后得到下正确的结果: 
53.             * 
54.             * 尊敬的客户刘明你好!本次消费金额$1000.00,您帐户888888888上的 
55.             * 余额为$1000000.00,欢迎下次光临! 
56.             * 
57.             * 另外,我们在这里使用Matcher对象的appendReplacement()方法来进行替换操作,而 
58.             * 不是使用String对象的replaceAll()或replaceFirst()方法来进行替换操作,因为 
59.             * 它们都能只能进行一次性简单的替换操作,而且只能替换成一样的内容,而这里则是要求每 
60.             * 一个匹配式的替换值都不同,所以就只能在循环里使用appendReplacement方式来进行逐 
61.             * 个替换了。 
62.             */ 
63.            matcher.appendReplacement(sb, value);  
64.            System.out.println("sb = " + sb.toString());  
65.        }  
66.        //最后还得要把尾串接到已替换的内容后面去,这里尾串为“,欢迎下次光临!”  
67.        matcher.appendTail(sb);  
68.        return sb.toString();  
69.    }  
70.}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics