![正则表达式:选择所有不包含另一个特定单词(标签)前面的某些特定单词的行](https://linux22.com/image/1578151/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%EF%BC%9A%E9%80%89%E6%8B%A9%E6%89%80%E6%9C%89%E4%B8%8D%E5%8C%85%E5%90%AB%E5%8F%A6%E4%B8%80%E4%B8%AA%E7%89%B9%E5%AE%9A%E5%8D%95%E8%AF%8D%EF%BC%88%E6%A0%87%E7%AD%BE%EF%BC%89%E5%89%8D%E9%9D%A2%E7%9A%84%E6%9F%90%E4%BA%9B%E7%89%B9%E5%AE%9A%E5%8D%95%E8%AF%8D%E7%9A%84%E8%A1%8C.png)
我想找到一个正则表达式来选择所有没有<p class="test_formal">
前面的行<span class="test_formal2">
<p class="test_formal"><span class="test_formal2">
我制作了一个正则表达式,但不太好。
(?s)\A(?!.*?(?<!\w)(<span class="test_formal2">)(?!\w)<p class="test_formal">).*
有人可以帮我一下吗?
答案1
这将匹配所有包含以下内容的行:
<span class="test_formal2">
或没有<p class="test_formal">
但<p class="test_formal">
随后<span class="test_formal2">
- Ctrl+F
- 找什么:
^(?:(?!<p class="test_formal">).)*<span class="test_formal2">.*$
- 检查环绕
- 检查正则表达式
- 取消选中
. matches newline
- Search in document
解释:
^ # beginning of line
(?: # start non capture group
(?! # negative lookahead, make sure we haven't
<p class="test_formal"> # literally
) # end lookahead
. # 1 any character
)* # end group, may appear 0 or more times
<span class="test_formal2"> # literally
.* # 0 or more any character
$ # end of line
鉴于:
<p class="test_formal">
<span class="test_formal2">
<p class="test_formal"><span class="test_formal2">
blah blah <p class="test_formal"><span class="test_formal2"> blah blah
<span class="test_formal2"><p class="test_formal">
blah blah <span class="test_formal2"><p class="test_formal"> blah blah
它匹配:
Search "^(?:(?!<p class="test_formal">).)*<span class="test_formal2">.*$" (3 hits in 1 file)
new 2 (3 hits)
Line 2: <span class="test_formal2">
Line 5: <span class="test_formal2"><p class="test_formal">
Line 6: blah blah <span class="test_formal2"><p class="test_formal"> blah blah