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:
- pair in question.- is wrapped with [^a-zA-Z]- or a space preceding the last -
- was preceded by a space.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:
-Wow-thank you-.-Wow- thank you-!- Wow-thank you-.- Wow!-thank you-- Wow -thank you--Wow - thank you--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.
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]+)
^^ ^^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With