Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex Query Help - 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:

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.

like image 570
Mike Wolfe Avatar asked May 14 '26 09:05

Mike Wolfe


1 Answers

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]+)
             ^^   ^^
like image 78
slackwing Avatar answered May 15 '26 23:05

slackwing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!