Regex Query Help - Lookbehind

。_饼干妹妹 提交于 2019-12-07 15:28:06

问题


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:

Conditions:

  • 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]
  • token must not be followed by a space on the first - or a space preceding the last -
    • "-Wow -" will not be a match as the closing - was preceded by a space.
    • See Sample test 6 and 7

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]+)

Sample test strings would be:

  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.

Thank you much!
mwolfe.


回答1:


Try a simpler middle expression.

(^|[\s\W]+)-(.*?)-($|[\s\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]+)
             ^^   ^^


来源:https://stackoverflow.com/questions/15288276/regex-query-help-lookbehind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!