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

320
Views
Match "[abc]", "[[abc]", "[abc]]", but not "[[abc]]",

I'm trying to match [abc] unless it's "escaped" by [] from both sides (so [[abc]] is considered as escaped, but not [[abc] or [abc]]).

Closest thing I could find is (?<!\[)\[abc\](?!\]) from Match "ABC" from *ABC*, but not from **ABC**, but it ignores match if it's escaped from only one side.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The lookahead could be either at the left, or at the right to allow a single backet on the left or right, but not a double square bracket on the other side.

(?<!\[)\[abc]|\[abc](?!])

Regex demo

over 4 years ago · Santiago Trujillo Report

0

@Thefourthbird's answer works, but would require that the main pattern abc be repeated, which goes against the DRY principle that most are encouraged to follow.

In the interest of maximizing code reuse, one approach would be to use a capture group to capture [abc] and then use it in a negative lookbehind pattern to ensure that it is not both preceded by a [ and followed by a ]:

(\[abc])(?<!\[\1(?=]))

Demo

Note that this works for C# because .NET happens to support variable-length lookbehind patterns, which aren't supported by many other regex engines.

over 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!