正则的具体写法不是重点,一般常用的还是比较简单的
主要想说的是在JAVA里使用正则的几种情况
先来定义两个变量:
1、被查找的字符串:str
2、要查找的关键字(或正则表达式):keywordPattern
情况一:判断str里是否含有keywordPattern
1
import
java.util.regex.Matcher;
2 import java.util.regex.Pattern;
3
4 public class RegTest {
5 public static void main(String[] args) {
6 String str = " 我是人。。我是好人。。我是好男人。。- - !! " ;
7 Pattern keywordPattern = Pattern.compile( " 好男人 " );
8 Matcher matcher = keywordPattern.matcher(str);
9 System.out.println(str.find());
10 }
11 }
2 import java.util.regex.Pattern;
3
4 public class RegTest {
5 public static void main(String[] args) {
6 String str = " 我是人。。我是好人。。我是好男人。。- - !! " ;
7 Pattern keywordPattern = Pattern.compile( " 好男人 " );
8 Matcher matcher = keywordPattern.matcher(str);
9 System.out.println(str.find());
10 }
11 }
输出:true
情况二:判断str是否完全符合keywordPattern,可用于邮箱验证等情况
1
public
class
RegTest {
2 public static void main(String[] args) {
3 String str = " abcd1234ABCD " ;
4 Pattern keywordPattern = Pattern.compile( " ^[a-zA-Z0-9]+$ " );
5 Matcher matcher = keywordPattern.matcher(str);
6
7 System.out.println(matcher.matches());
8 // System.out.println(matcher.find()); 也可以实现同样的效果
9 }
10 }
2 public static void main(String[] args) {
3 String str = " abcd1234ABCD " ;
4 Pattern keywordPattern = Pattern.compile( " ^[a-zA-Z0-9]+$ " );
5 Matcher matcher = keywordPattern.matcher(str);
6
7 System.out.println(matcher.matches());
8 // System.out.println(matcher.find()); 也可以实现同样的效果
9 }
10 }
输出:true
情况三:将str中符合keywordPattern的字符都替换掉
1
import
java.util.regex.Matcher;
2 import java.util.regex.Pattern;
3
4 public class RegTest {
5 public static void main(String[] args) {
6 String str = " abcd1234ABCD " ;
7 Pattern keywordPattern = Pattern.compile( " [0-9] " );
8 Matcher matcher = keywordPattern.matcher(str);
9
10 System.out.println(matcher.replaceAll( " @ " ));
11 }
12 }
2 import java.util.regex.Pattern;
3
4 public class RegTest {
5 public static void main(String[] args) {
6 String str = " abcd1234ABCD " ;
7 Pattern keywordPattern = Pattern.compile( " [0-9] " );
8 Matcher matcher = keywordPattern.matcher(str);
9
10 System.out.println(matcher.replaceAll( " @ " ));
11 }
12 }
输出:abcd@@@@ABCD
情况四:将str中符合keywordPattern的字符替换掉一部分,某些被替换的字符还需保留
1
import
java.util.regex.Matcher;
2 import java.util.regex.Pattern;
3
4 public class RegTest {
5 public static void main(String[] args) {
6 String str = " 12[3]456[7]890 " ;
7 Pattern keywordPattern = Pattern.compile( " \\[(\\d)\\] " );
8 Matcher matcher = keywordPattern.matcher(str);
9
10 System.out.println(matcher.replaceAll( " <$1> " ));
11 }
12 }
2 import java.util.regex.Pattern;
3
4 public class RegTest {
5 public static void main(String[] args) {
6 String str = " 12[3]456[7]890 " ;
7 Pattern keywordPattern = Pattern.compile( " \\[(\\d)\\] " );
8 Matcher matcher = keywordPattern.matcher(str);
9
10 System.out.println(matcher.replaceAll( " <$1> " ));
11 }
12 }
输出:12<3>456<7>890
情况五:将str中符合keywordPattern的字符替换掉一部分,某些被替换的字符还需做为参数进行处理
1
import
java.util.regex.Matcher;
2 import java.util.regex.Pattern;
3
4 public class RegTest {
5 public static void main(String[] args) {
6 String str = " a[b]cdef[g]hij[k]lmn " ;
7 Pattern keywordPattern = Pattern.compile( " \\[([a-z])\\] " );
8 Matcher matcher = keywordPattern.matcher(str);
9
10 StringBuffer strB = new StringBuffer();
11 while (matcher.find()){
12 matcher.appendReplacement(strB, getChar(matcher.group( 1 )));
13 }
14 matcher.appendTail(strB);
15 System.out.print(strB.toString());
16 }
17
18 public static String getChar(String num){
19 return " [ " + num.toUpperCase() + " ] " ;
20 }
21 }
2 import java.util.regex.Pattern;
3
4 public class RegTest {
5 public static void main(String[] args) {
6 String str = " a[b]cdef[g]hij[k]lmn " ;
7 Pattern keywordPattern = Pattern.compile( " \\[([a-z])\\] " );
8 Matcher matcher = keywordPattern.matcher(str);
9
10 StringBuffer strB = new StringBuffer();
11 while (matcher.find()){
12 matcher.appendReplacement(strB, getChar(matcher.group( 1 )));
13 }
14 matcher.appendTail(strB);
15 System.out.print(strB.toString());
16 }
17
18 public static String getChar(String num){
19 return " [ " + num.toUpperCase() + " ] " ;
20 }
21 }
输出:a[B]cdef[G]hij[K]lmn
其中第四和第五两种方法,可以实现 公式解析 和 模板解析 等复杂功能。
宠辱不惊,看庭前花开花落;去留无意,望天上云卷云舒