- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要获取一个字符串并提取模式的每个实例,并且只提取模式。
String test = "This is a test string to experience with regex by separing every instance of the word test and words that trail test";
所以现在该模式必须找到单词 test
以及它前后的任何不是 test
的单词。所以基本上它必须找到 3 个这种模式的实例。
我期望的 3 个结果如下:
这是一个测试字符串,用于通过分隔单词的每个实例来试验正则表达式
测试和跟踪的单词
测试
我在 gskinner 上尝试过正前瞻和负前瞻但还没有运气。
最佳答案
试试这个
(\s*\b(?!test\b)[a-z]+\b\s*)*test(\s*\b(?!test\b)[a-z]+\b\s*?)*
查看here on Regexr .
在 Java 中,我会将 [a-z]
替换为 \p{L}
,但 regexr 不支持 Unicode 属性。 \p{L}
是 Unicode code point with the property letter , 这将匹配任何语言的每个字母。
解释:
(\s*\b(?!test\b)[a-z]+\b\s*)*
匹配一系列不是“test”的单词。这是由否定先行断言 (?!test\b)
确保的。
test
匹配“test”
最后还是一样:再次匹配一系列不是“test”的单词 (\s*\b(?!test\b)[a-z]+\b\s*? )*
关于java - 正则表达式前瞻和后视匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14397263/
我正在尝试获得一个按钮,按下该按钮时会改变颜色。当再次按下时,它应该变回原来的颜色。我究竟做错了什么? 我的模板中的按钮: export default { data: {
我是一名优秀的程序员,十分优秀!