duanqian3464 2018-01-24 21:16
浏览 61
已采纳

正则表达式条件预测,下一个捕获组与PCRE匹配

I searched for answer and I did not found anything about that. hopefully that you can help me with my question.

So I try to search after string with lookahead conditional based on capture group at the end of a string. It means if the capture group at the end is a match, make the conditional group be with something and if capture group at the end is not a match so with something else.

See my regex in use here

(?:((?(?=ls)yes|no))\${(?:(?P<type>VAR)\s+)([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\s*\=\s*(\$\{CALL\s+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\s*\}|\"[^\"]*\"|'[^']*'|[0-9]*|(?:[fF]alse|[tT]rue))\s*\}(?<ls>[^\s]{1}))

Input:

    ${VAR foo="What"}x

    ${VAR foo="What"} 

    yes${VAR foo="What"}

    no${VAR foo="What"}x

As you see, it captures the word 'no' if there is something at the end as long it is not \s, but it did not capture the word 'yes' if it is nothing.

  • 写回答

1条回答 默认 最新

  • dsfdfdfd6576578 2018-01-24 21:50
    关注

    Your pattern contains (?(?=ls)yes|no) which is literally looking ahead for the characters ls. I've changed your pattern to utilize the DEFINE construct for subpattern reusability. As far as I'm aware, PCRE does not have a method to check whether or not a group was defined after the conditional. This can be accomplished in .net with the use of balancing groups, but PCRE doesn't employ those methods. PCRE does have the (?(name)yes|no) or (?(1)yes|no) conditional, but it doesn't work for forward references (comparative to testing whether or not a variable exists before it's even declared).

    See regex in use here

    (?(DEFINE)
      (?# var )
      (?<var>[a-zA-Z_\x7f-\xff][\w\x7f-\xff]*)
      (?# val )
      (?<val>(?&call)|(?&str)|(?&num)|(?&bool))
      (?<call>\$\{CALL\s+[a-zA-Z_\x7f-\xff][\w\x7f-\xff]*\s*\})
      (?<str>"[^"]*"|'[^']*')
      (?<num>\d+)
      (?<bool>(?i)(?:false|true)(?-i))
    )
    ((?(?=yes\${VAR\s+(?&var)\s*\=\s*(?&val)\s*\}\s)yes|no))
    \${(?P<type>VAR)\s+((?&var))\s*\=\s*((?&val))\s*\}(\S)?
    

    Without duplicating the subpattern in the positive lookahead, you can use the following (as seen in use here). The token (?8) recurses the 8th capture group:

    (?(DEFINE)
      (?# var )
      (?<var>[a-zA-Z_\x7f-\xff][\w\x7f-\xff]*)
      (?# val )
      (?<val>(?&call)|(?&str)|(?&num)|(?&bool))
      (?<call>\$\{CALL\s+[a-zA-Z_\x7f-\xff][\w\x7f-\xff]*\s*\})
      (?<str>"[^"]*"|'[^']*')
      (?<num>\d+)
      (?<bool>(?i)(?:false|true)(?-i))
    )
    ((?(?=no(?8)\S)no|yes))
    (\${(?P<type>VAR)\s+((?&var))\s*\=\s*((?&val))\s*\})(\S)?
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?