I'm using Sublime regex to convert a text like:
test! one.two .three; four, five . six. .seven
To look like:
test ! one . two . three ; four , five . six . . seven
When I tried using capture groups and replacing (\w)([^\w\s]+)|([^\w\s]+)(\w)
With: $1 $2
(notice the space), the string got totally messed up, with some chars being deleted.
test ! one .two hree ; four , five . six . even
Oddly enough, when I skip the pipelining and run the commands separately (replacing (\w)([^\w\s]+)
followed by a different replace for ([^\w\s]+)(\w)
), the result is as expected.
How do I pipeline ("or") regex variations and keep the grouping?
Thanks!
You are using 4 capturing groups using the alternation.
Looking at the example data, another option might be to use 2 capturing groups:
(\w+)\h*([^\w\s]+(?:\h+[^\w\s]+)*)\h*
Explanation
(\w+)
Capture group 1, match 1+ word chars\h*
Match 0+ horizontal whitespace chars(
Capture group 2
[^\w\s]+
Match any char except a word or whitespace char(?:\h+[^\w\s]+)*
Repeat the previous with 1+ horizontal whitespace chars prepended)
Close group 2\h*
Match 0+ trailing horizontal whitespace chars.In the replacement use $1 $2
Before (Using Sublime 3)
After
(?<=\w)(?=[^\w\s])|(?=\w)(?<=[^\w\s])