【发布时间】:2020-10-17 09:08:33
【问题描述】:
我要检查此文本 (file.txt):
_abcd
_efgh
#, _1
现在我只想匹配带有下划线但前面没有哈希# 的单词。万事俱备,我能做到
$perl -nle 'print $1 if /(_\w+)/' file.txt
但我不想匹配哈希,所以我会尝试后向:
$ perl -nle 'print $1 if /(?<!#.+)(_\w+)/' file.txt
Variable length lookbehind not implemented in regex m/(?<!#.+)(_\w+)/ at -e line 1.
- 如何在perl中实现变长lookbehind?
第二个,我会尝试先行:
$ perl -nle 'print $1 if /(?!#.+)(_\w+)/' file.txt
这将再次匹配所有内容,包括我不想要的 # 行。
- 如何匹配所有,除了
#行(换句话说,如何否定正则表达式)?
【问题讨论】:
-
试试
/#.+(*SKIP)(*F)|\b_\w+/ -
或者
^(?!.*#).*(_\w+) -
@WiktorStribiżew 你能解释一下吗?不知道
(*SKIP)或(*F)是什么意思,因为它解决了问题,但我不知道这个正则表达式变量 -
@JvdV,
_foo # bar失败
标签: regex perl regex-lookarounds regex-negation