Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

167
Views
Matching three underscore strings at the start of end of non-whitespace text chunks depending on immediate context

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.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!