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 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧