|
1 | 1 | package com.chen.api.util.regex; |
2 | 2 |
|
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.List; |
3 | 7 | import java.util.regex.Matcher; |
4 | 8 | import java.util.regex.Pattern; |
5 | 9 |
|
6 | 10 | /** |
7 | | - * 测试正则表达式类 |
| 11 | + * 测试正则表达式类 (//http://blog.csdn.net/allwefantasy/article/details/3136570/) |
8 | 12 | * |
9 | 13 | * @author Chen WeiJie |
10 | 14 | * @date 2017-11-16 21:31 |
11 | 15 | **/ |
12 | 16 | public class TestRegularExpression { |
13 | 17 |
|
14 | 18 |
|
15 | | - //http://blog.csdn.net/allwefantasy/article/details/3136570/ |
16 | | - public static void main(String[] args) { |
17 | 19 |
|
18 | | - if (args.length < 2) { |
19 | | - System.out.println("输入的字符串为空!"); |
20 | | - System.exit(0); |
| 20 | + @Test |
| 21 | + public void test1(){ |
| 22 | + |
| 23 | + String input = "This !! unusual use!!of exclamation!!points"; |
| 24 | + |
| 25 | + Pattern pattern =Pattern.compile("!!"); |
| 26 | + String [] arr = pattern.split(input); |
| 27 | + StringBuffer sb = new StringBuffer(); |
| 28 | + pattern.matcher(input).appendReplacement(sb,arr[0].toUpperCase()).appendTail(sb); |
| 29 | + |
| 30 | + String stringSpit ="this is a String!"; |
| 31 | + String[] arr2 = stringSpit.split("\\n"); |
| 32 | + |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | + @Test |
| 37 | + public void test2() { |
| 38 | + |
| 39 | + Matcher m = Pattern.compile("[frb][aiu][gx]").matcher("fix the rug with bags"); |
| 40 | + |
| 41 | + while (m.find()) { |
| 42 | + System.out.println(m.group()); |
21 | 43 | } |
22 | | - System.out.println("输入的字符串为: \"" + args[0] + "\""); |
23 | | - for (int i = 1; i < args.length; i++) { |
24 | | - System.out.println("正则表达式为: \"" + args[i] + "\""); |
25 | | - Pattern p = Pattern.compile(args[i]); |
26 | | - Matcher m = p.matcher(args[0]); |
27 | | - while (m.find()) { |
28 | | - System.out.println("Match \"" + m.group() + "\" at positions " + m.start() + "-" + (m.end() - 1)); |
29 | | - } |
| 44 | + |
| 45 | + m.reset("fix the rig with rags"); |
| 46 | + |
| 47 | + while (m.find()) { |
| 48 | + System.out.println(m.group()); |
30 | 49 | } |
| 50 | + |
| 51 | + |
31 | 52 | } |
32 | 53 |
|
33 | 54 |
|
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
34 | 59 | } |
0 commit comments