java - 为什么lookArounds的lookAround返回true,而匹配返回false
问题描述
我开始使用正则表达式中的环视
我认为正则表达式是 aq 的积极前瞻,然后是 u。但是当输入字符串为“qu”时,它不匹配。但是我得到了 lookingAt 函数的真实结果。
String regex="q(?=u)";
Pattern p= Pattern.compile(regex);
String test = "qu";
Matcher m= p.matcher(test);
System.out.println(m.matches());
System.out.println(m.lookingAt());
谁能解释为什么会这样?
解决方案
我认为这是因为它试图匹配整个字符串
火柴()
尝试将整个区域与模式匹配。
并且具有积极的前瞻性,我认为它与u
. 即q
如果它后面跟着一个u
. 因此,这q
不是整个测试字符串,也不匹配整个字符串。
这就是为什么你可以写这个来得到true
String regex="^q(?=u)u";
Pattern p= Pattern.compile(regex);
String test = "qu";
Matcher m= p.matcher(test);
System.out.println(m.matches());
System.out.println(m.lookingAt());
推荐阅读
- windows - 我可以设置运行程序限制以防止 Windows 计算机中的 fork 炸弹吗?
- java - 使用正则表达式拆分带有转义字符的消息(可以自我转义)
- python-3.x - 匹配两个lookbehinds之一
- python - 向现有 CSV 文件添加值
- docker - 如何将共享文件夹位置添加到我的应用程序(Docker)
- node.js - 使用 Mongo Migration 展平对象数据
- google-play-console - Google Play Console 打开测试发布报错“您需要添加完整描述”
- python - Django Crud 帮助 - 更新和删除
- vue.js - Vue 3 处理根元素内的脚本标签
- matlab - 如何从文件夹中读取单个图像,然后使用经过训练的神经网络对其进行分类?