原文链接: 正则表达式笔记 6 边界符中的 ^, $, \A, \Z, \z
字符边界主要是限定匹配的范围
目录标题
字符边界 ^ $
在默认情况下,^
匹配字符串的 开头 ,$
匹配字符串的 末尾 或字符串末尾的\n
之前 。
Pattern.MULTILINE(?m)
:多行模式会影响 ^
和 $
的行为。在默认情况下,^
和 $
分别匹配字符串的开始和结束。但在多行模式下,^
和 $
还可以分别匹配每行的开始和结束。
Matcher matcher = Pattern.compile(regex).matcher(str);
if (matcher.find()) {
System.out.println(matcher.group());
}
要匹配的字符串 | 正则表达式 | 单行匹配结果 | 多行匹配结果 |
---|---|---|---|
“Google\nApple” | “^Google\nApple” | “Google\nApple” | “Google\nApple” |
“Google\nApple\n” | “^Google\nApple” | “Google\nApple” | “Google\nApple” |
“\nGoogle\nApple\n” | “^Google\nApple” | ——————— | “Google\nApple” |
“Google\nApple\nBanana” | “^Google\nApple” | “Google\nApple” | “Google\nApple” |
“Google\nApple” | “Google\nApple$” | “Google\nApple” | “Google\nApple” |
“Google\nApple\n” | “Google\nApple$” | “Google\nApple” | “Google\nApple” |
“\nGoogle\nApple” | “Google\nApple$” | “Google\nApple” | “Google\nApple” |
“Banana\nGoogle\nApple” | “Google\nApple$” | “Google\nApple” | “Google\nApple” |
“Banana\nGoogle\nApple\nOrange” | “Google\nApple$” | ——————— | “Google\nApple” |
“Google\nApple” | “^Google\nApple$” | “Google\nApple” | “Google\nApple” |
“Google\nApple\n” | “^Google\nApple$” | “Google\nApple” | “Google\nApple” |
“\nGoogle\nApple” | “^Google\nApple$” | ——————— | “Google\nApple” |
“Google\nApple\nBanana” | “^Google\nApple$” | ——————— | “Google\nApple” |
“Google\nApple” | ^Google$ | ——————— | “Google” |
[Q&A] 加^$
和不加^$
的区别
加
^$
表示把要匹配的字符串当成一个整体做一次匹配。不加^$
则一个字符串可以匹配多次,只能代表这个字符串中有符合条件的,并不代表该字符串符合此条件。
字符边界 \A,\Z,\z
1、\A
等同于^
,无多行模式。仅匹配第一行的开头。
2、\Z
等同于$
,无多行模式。仅匹配最后一行的末尾,或末尾\n
之前。
3、\z
与 \Z
区别是: \z
不匹配字符串末尾的 \n
字符。
参考:正则表达式笔记 6 边界符中的 ^, $, \A, \Z, \z
Matcher matcher = Pattern.compile(regex, Pattern.MULTILINE).matcher(str);
if (matcher.find()) {
System.out.println(matcher.group());
}
要匹配的字符串 | 正则表达式 | 单行模式 | 正则表达式 | 单行模式 |
---|---|---|---|---|
“Google\nApple” | “\AGoogle\nApple” | “Google\nApple” | ||
“Google\nApple\n” | “\AGoogle\nApple” | “Google\nApple” | ||
“\nGoogle\nApple\n” | “\AGoogle\nApple” | ——————— | ||
“Google\nApple\nBanana” | “\AGoogle\nApple” | “Google\nApple” | ||
“Google\nApple” | “Google\nApple\Z” | “Google\nApple” | “Google\nApple\z” | “Google\nApple” |
“Google\nApple\n” | “Google\nApple\Z” | “Google\nApple” | “Google\nApple\z” | ——————— |
“\nGoogle\nApple” | “Google\nApple\Z” | “Google\nApple” | “Google\nApple\z” | “Google\nApple” |
“Banana\nGoogle\nApple” | “Google\nApple\Z” | “Google\nApple” | “Google\nApple\z” | “Google\nApple” |
“Banana\nGoogle\nApple\nOrange” | “Google\nApple\Z” | ——————— | “Google\nApple\z” | ——————— |
“Google\nApple” | “\AGoogle\nApple\Z” | “Google\nApple” | “\AGoogle\nApple\z” | “Google\nApple” |
“Google\nApple\n” | “\AGoogle\nApple\Z” | “Google\nApple” | “\AGoogle\nApple\z” | ——————— |
“\nGoogle\nApple” | “\AGoogle\nApple\Z” | ——————— | “\AGoogle\nApple\z” | ——————— |
“Google\nApple\nBanana” | “\AGoogle\nApple\Z” | ——————— | “\AGoogle\nApple\z” | ——————— |
“Google\nApple” | “\AGoogle\Z” | ——————— | “\AGoogle\z” | ——————— |
字符边界 \b,\B
\b
:匹配单词字符(\w
)和非单词字符(\W
)之间的边界,也可以匹配字符串开头
或结尾
处的字边界。
\B
:\B
与\b
截然相反,\B=[^\b]
。
通过一个例子理解 字符边界 \b
的作用
通过一个例子理解 字符边界 \B
的作用
[Q&A] \b・\B使用场景
一般\b
,\B
不用来判断当前字符串是否符合某种规则,一般用\b
,\B
来进行获取。 [Ref] :详解正则表达式中的\B和\b
要匹配的字符串 | 正则表达式 | 单行模式 | 正则表达式 | 单行模式 |
---|---|---|---|---|
" 目标字段 " | “\b目标字段\b” | 目标字段 | “\B目标字段\B” | ——————— |
" 目标字段! " | “\b目标字段\b” | 目标字段 | “\B目标字段\B” | ——————— |
" 目标字段1 " | “\b目标字段\b” | ——————— | “\B目标字段\B” | ——————— |
" 目标字段哈 " | “\b目标字段\b” | ——————— | “\B目标字段\B” | ——————— |
" 哈哈目标字段哈 " | “\b目标字段\b” | ——————— | “\B目标字段\B” | 目标字段 |
字符边界 \G
\G 匹配必须从上一个匹配结束的位置开始
不加入\G,则只返回第一个匹配,无论执行多少次均是如此;
如加入\G,则第一次执行返回第一个匹配,再执行返回第二个匹配,依次类推
没研究明白,感觉java自身就可以挨个遍历到
String str = "user18dsdfuser2dsfsduser32323hjkuser555";
Matcher matcher = Pattern.compile("user\\d*").matcher(str);
while (matcher.find()) {
String group = matcher.group();
System.out.println(group);
}
user18
user2
user32323
user555user18
user2