正则字符边界

本文详细介绍了正则表达式中的边界符,包括^匹配字符串开头,$匹配字符串末尾,A始终匹配字符串开始,和z区别在于对末尾换行符的处理,匹配单词边界,B则匹配非单词边界,以及特殊边界符G用于连续匹配。这些概念对于精确匹配和搜索文本至关重要。
摘要由CSDN通过智能技术生成

原文链接: 正则表达式笔记 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值