正则表达式定义了字符串的模式。
正则表达式可以用来搜索、编辑或处理文本。
正则表达式并不仅限于某一种语言,但是在每种语言中有细微的差别。
捕获组
捕获组是把多个字符当一个单独单元进行处理的方法,它通过对括号内的字符分组来创建。
捕获组是通过从左至右计算其开括号来编号。例如,在表达式((A)(B(C))),有四个这样的组:
- ((A)(B(C)))
- (A)
- (B(C))
- (C)
正则表达式语法
\ |
将下一字符标记为特殊字符、文本、反向引用或八进制转义符。例如, n匹配字符 n。\n 匹配换行符。序列 \\\\ 匹配 \\ ,\\( 匹配 (。 |
^ |
匹配输入字符串开始的位置。 |
$ |
匹配输入字符串结尾的位置。 |
* |
零次或多次匹配前面的字符或子表达式。例如,zo* 匹配"z"和"zoo"(o可以匹配至少0次)。* 等效于 {0,}。 |
+ |
一次或多次匹配前面的字符或子表达式。例如,"zo+"与"zo"和"zoo"匹配(o可以匹配一次或多次),但与"z"不匹配。+ 等效于 {1,}。 |
? |
零次或一次匹配前面的字符或子表达式。例如,"do(es)?"匹配"do"或"does"中的"do"(es出现0次或1次)。? 等效于 {0,1}。 |
{ n} |
n 是非负整数。正好匹配 n 次。例如,"o{2}"与"Bob"中的"o"不匹配,但与"food"中的两个"o"匹配。 |
{ n,} |
n 是非负整数。至少匹配 n 次。例如,"o{2,}"不匹配"Bob"中的"o",而匹配"foooood"中的所有 o。"o{1,}"等效于"o+"。"o{0,}"等效于"o*"。 |
{ n,m} |
m 和 n 是非负整数,其中 n <= m。匹配至少 n 次,至多 m 次。例如,"o{1,3}"匹配"fooooood"中的头三个 o。'o{0,1}' 等效于 'o?'。 |
? |
当此字符紧随任何其他限定符(*、+、?、{ n}、{ n,}、{ n,m})之后时,匹配模式是"非贪心的"。"非贪心的"模式匹配搜索到的、尽可能短的字符串,而默认的"贪心的"模式匹配搜索到的、尽可能长的字符串。例如,在字符串"oooo"中,"o+?"只匹配单个"o",而"o+"匹配所有"o"。 |
. |
匹配除"\r\n"之外的任何单个字符。若要匹配包括"\r\n"在内的任意字符,请使用诸如"[\s\S]"之类的模式。 |
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"范围内的任何字符。 |
\b |
匹配一个字边界,即字与空格间的位置。例如,"er\b"匹配"never"中的"er",但不匹配"verb"中的"er"。 |
\B |
非字边界匹配。"er\B"匹配"verb"中的"er",但不匹配"never"中的"er"。 |
\d |
数字字符匹配。等效于 [0-9]。 |
\D |
非数字字符匹配。等效于 [^0-9]。 |
\f |
换页符匹配。等效于 \x0c 和 \cL。 |
\n |
换行符匹配。等效于 \x0a 和 \cJ。 |
\r |
匹配一个回车符。等效于 \x0d 和 \cM。 |
\s |
匹配任何空白字符,包括空格、制表符、换页符等。与 [ \f\n\r\t\v] 等效。 |
\S |
匹配任何非空白字符。与 [^ \f\n\r\t\v] 等效。 |
\t |
制表符匹配。与 \x09 和 \cI 等效。 |
\v |
垂直制表符匹配。与 \x0b 和 \cK 等效。 |
\w |
匹配任何字类字符,包括下划线。与"[A-Za-z0-9_]"等效。 |
\W |
与任何非单词字符匹配。与"[^A-Za-z0-9_]"等效。 |
- Pattern 类:
pattern 对象是一个正则表达式的编译表示。传入需要匹配的字串。
Pattern p = Pattern.compile(REGEX);//创建正则表达式对象
- Matcher 类:
Matcher 对象是对输入字符串进行解释和匹配操作的引擎。传入待查找的字串
Matcher m = p.matcher(INPUT); // 获取 matcher 对象
Matcher类方法:
1、索引方法
1 | public int start() 返回以前匹配的初始索引。 |
2 | public int end() 返回最后匹配字符之后的偏移量。 |
public class lian1 {
private static final String REGEX = "\\bcat\\b";//含有边界空格的cat
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);//4个 cat cat cat cat
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
2、查找方法
1 | public boolean lookingAt() 尝试将从区域开头开始的输入序列与该模式匹配。 |
2 | public boolean find() 尝试查找与该模式匹配的输入序列的下一个子序列。 |
3 | public boolean matches() 尝试将整个区域与模式匹配。 |
public class lian1 {
private static final String REGEX = "foo";
private static final String INPUT = "food";
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: food
Current INPUT2 is: ooooofoooooooooooo
lookingAt(): true
matches(): false
lookingAt(): false
3、替换方法
1 | public String replaceAll(String replacement) 替换模式与给定替换字符串相匹配的输入序列的每个子序列。 |
2 | public String replaceFirst(String replacement) 替换模式与给定替换字符串匹配的输入序列的第一个子序列。 |
public class lian1 {
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.