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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么