问题
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]
- must not be a normal character [a-zA-Z] surrounding the
- 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
- "-Wow -" will not be a match as the closing
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:
- (all.):
-Wow-thank you-.
- (Wow):
-Wow- thank you-!
- (NIL):
- Wow-thank you-.
- (thank you):
- Wow!-thank you-
- (thank you):
- Wow -thank you-
- (all):
-Wow - thank you-
- (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