Home > Mobile >  Matching three underscore strings at the start of end of non-whitespace text chunks depending on imm
Matching three underscore strings at the start of end of non-whitespace text chunks depending on imm

Time:12-25

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.
  • Related