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)?
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。