繁体   English   中英

有人可以解释一下此正则表达式吗? ^(?=。* prodCode =)。* $

[英]Can someone please explain this regex? ^(?=.*prodCode=).*$

有人可以解释一下此正则表达式吗?

^(?=.*prodCode=).*$

这个很好的正则表达式解释器中

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ^                      the beginning of the string
--------------------------------------------------------------------------------
  (?=                    look ahead to see if there is:
--------------------------------------------------------------------------------
  .                      any character except \n
--------------------------------------------------------------------------------
  prodCode=              'prodCode='
--------------------------------------------------------------------------------
  )                      end of look-ahead
--------------------------------------------------------------------------------
  .                      any character except \n
--------------------------------------------------------------------------------
  $                      before an optional \n, and the end of the string

编辑

由于问题文本中的正则表达式已更改,因此解释中的倒数第二行将变为:

--------------------------------------------------------------------------------
  .*                     any character except \n (0 or more times (matching
                         the most amount possible))
--------------------------------------------------------------------------------

从行searhinng位置的开头开始, prodCode=之前的任何符号。 (?=)表示仅检查位置不匹配。 因此,在您的情况下,如果该行中存在像any symbol + prodCode=这样的字符串,则如果不匹配则匹配整行,然后返回false。

如果字符串中的任何地方都有prodCode= ,则此方法匹配,并且匹配完整的字符串。

编写它的另一种方式(粗略地讲,将方法返回值滥用为regex matcch)是

if (s.indexOf("prodCode=") != -1)
    return s;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM