正则表达式仅在字符序列之后匹配出现 [英] Regex match occurrences only after character sequence
本文介绍了正则表达式仅在字符序列之后匹配出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想找到这个字符串中出现在 w 之后的所有字符,但只找到 foo
I want to find all the characters after w's that occur in this string, but only the ones after foo
edward woodward foo edward woodward
/(?<=w)./g
给了我 6 个匹配
edward woodward foo edward woodward
edward woodward foo edward woodward
我只想要 foo
之后发生的 3 个匹配项.我将如何修改正则表达式以缩小搜索范围?
I only want the 3 matches that occur after foo
. How would I modify the regex to narrow the scope of the search?
推荐答案
您可以在 PCRE 引擎中使用以下正则表达式:
You may use the following regex with a PCRE engine:
(?:\bfoo\b|\G(?!^))[^w]*w\K.
查看正则表达式演示.
详情
(?:\bfoo\b|\G(?!^))
- 或者一个完整的词foo
(\bfoo\b
) 或 (|
) 前一个匹配的结尾 (\G(?!^)
)[^w]*
- 除w
之外的任何 0+ 个字符w
-w
字符\K
- 匹配重置操作符丢弃所有匹配的文本.
- 任何字符(换行符除外,如果您需要将它们与.
匹配,请添加(?s)
模式开始或替换.
为(?s:.)
)
(?:\bfoo\b|\G(?!^))
- either a whole wordfoo
(\bfoo\b
) or (|
) the end of the previous match (\G(?!^)
)[^w]*
- any 0+ chars other thanw
w
- aw
char\K
- match reset operator discarding all text matched so far.
- any char (other than line break chars, if you need to match them with.
add(?s)
at the pattern start or replace.
with(?s:.)
)
这篇关于正则表达式仅在字符序列之后匹配出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文