Java 正则表达式

正则表达式 描述
this is text 匹配字符串 “this is text”
this\s+is\s+text 注意字符串中的 \s+。
匹配单词 “this” 后面的 \s+ 可以匹配多个空格,之后匹配 is
可以匹配这个实例:this is text
^\d+(.\d+)? ^ 定义了以什么开始
\d+ 匹配一个或多个数字
? 设置括号内的选项是可选的
. 匹配 “.”
可以匹配的实例:”5”, “1.5” 和 “2.21”。

以下实例中使用了正则表达式 .runoob. 用于查找字符串中是否包了 runoob 子串:

import java.util.regex.*;
class RegexExample1{
public static void main(String[] args){
String content = "I am noob " +"from runoob.com.";
String pattern = ".*runoob.*";
boolean isMatch = Pattern.matches(pattern, content);
System.out.println("字符串中是否包含了 'runoob' 子字符串? " + isMatch);
}
}

捕获组

捕获组是把多个字符当一个单独单元进行处理的方法,它通过对括号内的字符分组来创建。

有点麻烦,暂时不写

正则表达式语法

System.out.print(“\“); // 输出为
System.out.print(“\\“); // 输出为 \

符号`\`

> 将下一字符标记为特殊字符、文本、反向引用或八进制转义符。例如, n匹配字符 n。\n 匹配换行符。序列 \\\\ 匹配 \\ ,\\( 匹配 (。

^
匹配输入字符串开始的位置。

$
匹配输入字符串结尾的位置。

*
零次或多次匹配前面的字符或子表达式。例如,zo* 匹配"z""zoo"。* 等效于 {0,}。

+
一次或多次匹配前面的字符或子表达式。例如,"zo+""zo""zoo"匹配,但与"z"不匹配。+ 等效于 {1,}。

?
零次或一次匹配前面的字符或子表达式。例如,"do(es)?"匹配"do""does"中的"do"。? 等效于 {0,1}。

{n}
n 是非负整数。正好匹配 n 次。例如,"o{2}""Bob"中的"o"不匹配,但与"food"中的两个"o"匹配。

?
当此字符紧随任何其他限定符(*、+、?、{n}、{n,}、{n,m})之后时,匹配模式是"非贪心的""非贪心的"模式匹配搜索到的、尽可能短的字符串,而默认的"贪心的"模式匹配搜索到的、尽可能长的字符串。例如,在字符串"oooo"中,"o+?"只匹配单个"o",而"o+"匹配所有"o"

.
匹配除"\r\n"之外的任何单个字符。

x|y
匹配 x 或 y。例如,'z|food' 匹配"z""food"'(z|f)ood' 匹配"zood""food"

[xyz]
字符集。匹配包含的任一字符。例如,"[abc]"匹配"plain"中的"a"

[^xyz]
反向字符集。匹配未包含的任何字符。例如,"[^abc]"匹配"plain""p""l""i""n"

[a-z]
字符范围。匹配指定范围内的任何字符。例如,"[a-z]"匹配"a""z"范围内的任何小写字母。

[^a-z]
反向范围字符。匹配不在指定的范围内的任何字符。例如,"[^a-z]"匹配任何不在"a""z"范围内的任何字符。

\d
数字字符匹配。等效于 [0-9]。

\D
非数字字符匹配。等效于 [^0-9]。

\s
匹配任何空白字符,包括空格、制表符、换页符等。与 [ \f\n\r\t\v] 等效。

\S
匹配任何非空白字符。与 [^ \f\n\r\t\v] 等效。

\w
匹配任何字类字符,包括下划线。与"[A-Za-z0-9_]"等效。

\W
与任何非单词字符匹配。与"[^A-Za-z0-9_]"等效。

Matcher 类的方法

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatches
{
private static final String REGEX = "\\bcat\\b";
private static final String INPUT =
"cat cat cat cattie cat";

public static void main( String[] args ){
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(INPUT); // 获取 matcher 对象
int count = 0;

while(m.find()) {
count++;
System.out.println("Match number "+count);
System.out.println("start(): "+m.start());
System.out.println("end(): "+m.end());
}
}
}

Match number 1
start(): 0
end(): 3
Match number 2
start(): 4
end(): 7
Match number 3
start(): 8
end(): 11
Match number 4
start(): 19
end(): 22

matches 和 lookingAt 方法

matches 和 lookingAt 方法都用来尝试匹配一个输入序列模式。它们的不同是 matches 要求整个序列都匹配,而lookingAt 不要求。
lookingAt 方法虽然不需要整句都匹配,但是需要从第一个字符开始匹配。

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatches
{
private static final String REGEX = "foo";
private static final String INPUT = "fooooooooooooooooo";
private static final String INPUT2 = "ooooofoooooooooooo";
private static Pattern pattern;
private static Matcher matcher;
private static Matcher matcher2;

public static void main( String[] args ){
pattern = Pattern.compile(REGEX);
matcher = pattern.matcher(INPUT);
matcher2 = pattern.matcher(INPUT2);

System.out.println("Current REGEX is: "+REGEX);
System.out.println("Current INPUT is: "+INPUT);
System.out.println("Current INPUT2 is: "+INPUT2);


System.out.println("lookingAt(): "+matcher.lookingAt());
System.out.println("matches(): "+matcher.matches());
System.out.println("lookingAt(): "+matcher2.lookingAt());
}
}

Current REGEX is: foo
Current INPUT is: fooooooooooooooooo
Current INPUT2 is: ooooofoooooooooooo
lookingAt(): true
matches(): false
lookingAt(): false

replaceFirst 和 replaceAll 方法

replaceFirst 和 replaceAll 方法用来替换匹配正则表达式的文本。不同的是,replaceFirst 替换首次匹配,replaceAll 替换所有匹配。

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatches
{
private static String REGEX = "dog";
private static String INPUT = "The dog says meow. " +
"All dogs say meow.";
private static String REPLACE = "cat";

public static void main(String[] args) {
Pattern p = Pattern.compile(REGEX);
// get a matcher object
Matcher m = p.matcher(INPUT);
INPUT = m.replaceAll(REPLACE);
System.out.println(INPUT);
}
}

The cat says meow. All cats say meow.

appendReplacement 和 appendTail 方法

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatches
{
private static String REGEX = "a*b";
private static String INPUT = "aabfooaabfooabfoobkkk";
private static String REPLACE = "-";
public static void main(String[] args) {
Pattern p = Pattern.compile(REGEX);
// 获取 matcher 对象
Matcher m = p.matcher(INPUT);
StringBuffer sb = new StringBuffer();
while(m.find()){
m.appendReplacement(sb,REPLACE);
}
m.appendTail(sb);
System.out.println(sb.toString());
}
}

PatternSyntaxException 类的方法

不学