正则表达式是一个很强大的模式语言,使用它我们能够解决很多很棘手的问题,有时候使用字符串查找来解决这类问题不是很方便,所以这个时候正则表达式就能帮我们很大的忙。
完整的正则表达式由两种字符构成。特殊字符(specialcharacters,比如*)
称为“元字符”(metacharacters),其他为“文字”(literal),或者是普通文本字符(normaltext characters).
编号在 0 ~ 255 范围的字符,比如:空格可以使用 “\x20” 表示
|
\uXXXX
|
任何字符可以使用 “\u” 再加上其编号的4位十六进制数表示,比如:”\u4E2D”
|
任何 “空白”字 符(例如空格符、制表符、进纸符等)
|
除\s 之外的任何字符
|
a-zA-ZO-9
|
除\w之外的任何字符,也就是[Aa-zA-ZO-9]
|
[0-9],即数字
|
除\d以外的任何字符,即[^a-zA-Z0-9]
|
匹配非单词边界,即左右两边都是 “\w” 范围或者左右两边都不是 “\w” 范围时的字符缝隙
|
匹配单词边界
|
1 2
|
java.util.regex.Pattern java.util.regex.Matcher
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
public class RegexTest { public static void main(String[] args) { String myTest = "http://localhost:8080/spring/swagger-ui.html"; String myRegex = "http://(\\w+)(?<port>:\\d+)"; Pattern pattern = Pattern.compile(myRegex); Matcher matcher = pattern.matcher(myTest); int groupCount = matcher.groupCount(); System.out.println("groupCount = " + groupCount); if(matcher.find()) { System.out.println(matcher.group(0)); System.out.println(matcher.group(1)); System.out.println(matcher.group("port")); } } }
输出如下: groupCount = 2 http: localhost :8080
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
public class RegexTest { public static void main(String[] args) { String myTest = "http://localhost:8080/spring/swagger-ui.html"; String myRegex = "http://(\\w+)(?::\\d+)"; Pattern pattern = Pattern.compile(myRegex); Matcher matcher = pattern.matcher(myTest); int groupCount = matcher.groupCount(); System.out.println("groupCount = " + groupCount); if(matcher.find()) { System.out.println(matcher.group(0)); System.out.println(matcher.group(1)); } } }
输出如下: groupCount = 1 http: localhost
|
1 2 3 4 5 6
|
String str = "hello world,hello java";
System.out.println(str.replaceAll("(hello)", "$1 my"));
输出: hello my world,hello my java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
public class RegexTest { public static void main(String[] args) { String myTest = "hello java"; String myRegex = "\\w+"; Pattern pattern = Pattern.compile(myRegex); Matcher matcher = pattern.matcher(myTest); matcher.reset("hello 1998\r\n hello 2018"); matcher.usePattern(Pattern.compile("\\d+")); while(matcher.find()) { System.out.println(matcher.group()); } } } 输出如下: 1998 2018
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
public class RegexTest { public static void main(String[] args) { String myTest = "我是中国人 I'm chinese"; String myRegex = "[^\\u4e00-\\u9fa5]+"; Pattern pattern = Pattern.compile(myRegex); Matcher matcher = pattern.matcher(myTest); while (matcher.find()) { System.out.println(matcher.group()); } } } 输出如下: I'm chinese
|
1 2 3 4 5 6
|
String myTest = "我要要学学学Jaaaaaava"; String myRegex = "(.)\\1+"; System.out.println(myTest.replaceAll(myRegex, "$1"));
输出如下: 我要学Java
|