来自此文件:
xxxxxxx; foo("this should be extracted 1"); bar("this should not be extracted"); yyyyyyy
zzzzzzzzzzzzzzzzzzzzzzzz foo("this should be extracted 2") uuuuuuuuuuuuuuuuuuuuuuuuuuuuu
我想要得到这个:
this should be extracted 1
this should be extracted 2
我只能使用这个正则表达式来选择我想要提取的字符串:(?<=foo\(")(.*?)(?="\))
。但我找不到提取它或删除所有未标记文本的方法。
我在示例中添加了一些x, y, z
和u
字符,只是为了表明在这个文件中的同一行中还有很多其他内容
有人能帮助我吗?
谢谢!
答案1
您可以使用^(?:(?!foo\(\").)*$
删除所有不包含的行foo("
然后^\r\n
删除空行
然后使用^.*foo\(\"(.*?)\"\).*
替换所有\1
答案2
- Ctrl+H
- 找什么:
(?:^|\G).*?foo\("(.+?)"\)(?:(?!\bfoo\().)*
- 用。。。来代替:
$1
- 检查环绕
- 检查正则表达式
- 取消选中
. matches newline
- Replace all
解释:
(?:^|\G) # beginning of line OR restart from last match position
.*? # 0 or more any character but newline, not greedy
foo\(" # literally
(.+?) # group 1, 1 or more any character but newline not greedy, what to be extracted
"\)
(?:(?!\foo\().)* # Tempered greedy token, make sure we haven't "foo" after
替代品:
$1 # content of group 1, what to be extracted
给定示例的结果:
this should be extracted 1
this should be extracted 2
屏幕截图: