正则表达式查询帮助 - Lookbehind [英] Regex Query Help - Lookbehind

查看:137
本文介绍了正则表达式查询帮助 - Lookbehind的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与以下内容有关:正则表达式 - 格式化文本阻止 - 即时消息但是另一个问题。

This is somewhat related to: Regular Expression - Formatting text in a block - IM but a different problem.

寻找 - 的包装文字如下条件:

Looking for -'s wrapping text with the following conditions:

条件:


  • 令牌可以在行的开头或结尾

  • 令牌必须按空格或一个或多个符号包围:{。,!@#$ ....}。


    • 一定不能是有问题的 - 对的普通字符[a-zA-Z] 。

    • 参见示例测试3 ... w-谢谢 -

    • 测试4和5成功,因为 - 包裹着[^ a-zA-Z]

    • token can be at start or end of line
    • token must be surround by space or one or more symbols: {.,!@#$....}.
      • must not be a normal character [a-zA-Z] surrounding the - pair in question.
      • See Sample test 3 ...w-thank you-
      • Test 4 and 5 succeed because the - is wrapped with [^a-zA-Z]

      • -​​ 哇 - 不会匹配,因为结束 - 前面有一个空格。

      • 参见样本测试6和7

      • "-Wow -" will not be a match as the closing - was preceded by a space.
      • See Sample test 6 and 7

      对于正则表达式的前面我需要:(^ | [\\\\ W] +)

      ,结尾将是:($ | [\\ \\ s \ W] +)

      For the front of the regular expression I would need: (^|[\s\W]+)
      and the end would be: ($|[\s\W]+)

      我有当前的表达式,但由于在找到之后停止转义条件,它失败了第一个 -

      I have the current expression, but it is failing due to the escape condition being stop after finding the first -

         (^|[\s\W]+)-([^\s][^-]*)-($|[\s\W]+)
      

      示例测试字符串为:


      1. (全部。): -Wow - 谢谢 - 。

      2. (哇): - 哇 - 谢谢 - !

      3. (NIL): - 哇 - 谢谢 - 。

      4. (谢谢): - 哇! - 谢谢你 -

      5. (谢谢): - 哇 - 谢谢你 -

      6. (全部): - 哇 - 谢谢 -

      7. (NIL ): - 哇 - 谢谢 -

      1. (all.): -Wow-thank you-.
      2. (Wow): -Wow- thank you-!
      3. (NIL): - Wow-thank you-.
      4. (thank you): - Wow!-thank you-
      5. (thank you): - Wow -thank you-
      6. (all): -Wow - thank you-
      7. (NIL): -Wow - thank you -

      这需要看后面吗? (我是一个正念新手,所以请耐心等待)或者我的中间情况完全错了。

      Does this require look behind? (I'm a regex newbie so please bear with me) Or is my middle condition totally wrong.

      非常感谢!

      mwolfe。

      Thank you much!
      mwolfe.

      推荐答案

      尝试更简单的中间表达式。

      Try a simpler middle expression.

      (^|[\s\W]+)-(.*?)-($|[\s\W]+)
                   ^^^
      

      非贪婪的通配符匹配将捕获匹配以下所需的最小字符串 - ($ | [\\\\ W] +)

      The non-greedy wildcard match would capture the minimum string necessary to match the following -($|[\s\W]+).

      编辑。好的,我明白为什么这是错的。您希望非空格字符分别立即跟随并成功打开和关闭破折号。所以试试这个:

      Edit. Okay, I see why that's wrong. You want a non-space character to immediately follow and succeed the opening and closing dashes, respectively. So try this:

      (^|[\s\W]+)-(\S.*?\S)-($|[\s\W]+)
                   ^^   ^^
      

      这篇关于正则表达式查询帮助 - Lookbehind的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆