Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何用lookback否定perl正则表达式?_Regex_Perl_Regex Lookarounds_Regex Negation - Fatal编程技术网

Regex 如何用lookback否定perl正则表达式?

Regex 如何用lookback否定perl正则表达式?,regex,perl,regex-lookarounds,regex-negation,Regex,Perl,Regex Lookarounds,Regex Negation,我要检查以下文本(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/(?&

我要检查以下文本(
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.
这将再次匹配所有,包括我不想要的
#

  • 如何匹配除
    #
    行以外的所有行(换句话说,如何否定正则表达式)
  • 你可以用

    /#.+(*SKIP)(*F)|_\w+/
    
    或者,要在单词边界处匹配

    /#.+(*SKIP)(*F)|\b_\w+/
    
    模式匹配

    • #+(*SKIP)(*F)
      -
      #
      和尽可能多的除换行符以外的任何1个或多个字符,然后跳过、省略、丢弃匹配,并从跳过匹配的位置搜索下一个匹配
    • |
      -或
    • \uw+
      -a
      \uu
      然后是任意一个或多个单词字符
      • 以下操作即可:

        /^[^#]*(_\w+)/
        

        请尝试
        /#.+(*跳过)(*F)|\b\u\w+/
        或者
        ^(?。*).*(\u\w+)
        @WiktorStribiż您能解释一下吗?我不知道
        (*SKIP)
        (*F)
        是什么意思,因为它解决了问题,但我不知道这个正则表达式variables@JvdV,对于
        \u foo#bar
        则失败,因此通过
        (*SKIP)
        进行“丢弃”,而
        (*F)
        表示“从该点继续”?或者
        (*F)
        有什么作用?@milanHrabos
        (*SKIP)(*FAIL)
        就是这样工作的,跳过匹配的文本,
        (*F)
        /
        (*FAIL)
        会立即导致失败。这一切也都在@milanHrabos中,
        (*F)
        (*FAIL)
    可以替换为
    (?!)
    ,它也会在使用它的地方导致故障。
    /^[^#]*(_\w+)/