In all cases listed below, I want to match only ___
(three underscores).
To make it clear, I am trying to match 3 underscores either not immediately preceded or not immediately followed with an underscore character and:
- if a character other than whitespace and
_
precedes the___
, match last 3 (test3____) - if a character other than whitespace and
_
follows the___
, match first 3 (_______test5).
See my current attempt:
((?<=_)___|___(?=_)|___)
Tested with:
test1___(test2 ___)
test3____ ___
___test4 ____test5
All cases are matching correctly except for "test3": it matches first 3 underscores instead of the last 3.
CodePudding user response:
You can use
(?<!_)___(?=_*[^\s_])|(?<=[^\s_]_*)___(?!_)|(?<!_)___(?!_)
See the regex demo. Details:
(?<!_)___(?=_*[^\s_])
- a___
string not immediately preceded with a_
char and that is immediately followed with any zero or more underscores and then any char other than whitespace and_
|
- or(?<=[^\s_]_*)___(?!_)
- a___
string that is not immediately followed with another_
char and immediately preceded with any char other than whitespace and_
and then any zero or more underscores|
- or(?<!_)___(?!_)
- three underscores neither preceded nor followed with another_
char.