Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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 使用lookahead反转正则表达式以查找匹配的字符串_Regex - Fatal编程技术网

Regex 使用lookahead反转正则表达式以查找匹配的字符串

Regex 使用lookahead反转正则表达式以查找匹配的字符串,regex,Regex,我有这个正则表达式(由其他人提供),我需要使用匹配的字符串进行测试 "(?<=[H|h]at [\'|\""]).*?(?=[\'|\""])" (?其中包括: 而实际匹配的文本是分隔符字符之间的任何字符((“|“|”、“|\\|”)) 值得一提的是,如果您有机会在您的代码库中重写此代码,现在可能是一个合适的时机。:)让我们来分析一下 首先,看起来我们正在努力确保单词“hat”或“hat”后面有一个单独的引语出现在我们匹配的任何东西之前 (?我注意到的第一件事是,正则表达式显然是由不

我有这个正则表达式(由其他人提供),我需要使用匹配的字符串进行测试

"(?<=[H|h]at [\'|\""]).*?(?=[\'|\""])"
(?其中包括:

而实际匹配的文本是分隔符字符之间的任何字符(
(“|“|”、“|\\|”)


值得一提的是,如果您有机会在您的代码库中重写此代码,现在可能是一个合适的时机。:)

让我们来分析一下


首先,看起来我们正在努力确保单词“hat”或“hat”后面有一个单独的引语出现在我们匹配的任何东西之前


  • (?我注意到的第一件事是,正则表达式显然是由不知道自己在做什么的人编写的,因此我建议将它扔掉,自己编写。
    
    Hat 'pretty much anything in apostrophes'
    hat 'pretty much anything in apostrophes'
    |at 'pretty much anything in apostrophes'
    Hat |pretty much anything in pipes|
    hat |pretty much anything in pipes|
    |at |pretty much anything in pipes|
    Hat "pretty much anything in double quotes"
    hat "pretty much anything in double quotes"
    |at "pretty much anything in double quotes"
    Hat ""pretty much anything in double double quotes""
    hat ""pretty much anything in double double quotes""
    |at ""pretty much anything in double double quotes""
    Hat "and pretty much anything in any combination of those|
    hat |and pretty much anything in any combination of those'
    |at ""and pretty much anything in any combination of those"
    
    [Hh]at\s['"]
    
    (?=['"])