61497

Regular expression to match single bracket pairs but not double bracket pairs

Is it possible to make a regular expression to match everything within single brackets but ignore double brackets, so for example in:

{foo} {bar} {{baz}}

I'd like to match foo and bar but not baz?

Answer1:

To only match foo and bar without the surrounding braces, you can use

(?<=(?<!\{)\{)[^{}]*(?=\}(?!\}))

if your language supports lookbehind assertions.

<strong>Explanation:</strong>

(?<= # Assert that the following can be matched before the current position (?<!\{) # (only if the preceding character isn't a {) \{ # a { ) # End of lookbehind [^{}]* # Match any number of characters except braces (?= # Assert that it's possible to match... \} # a } (?!\}) # (only if there is not another } that follows) ) # End of lookahead

<strong>EDIT:</strong> In JavaScript, you don't have lookbehind. In this case you need to use something like this:

var myregexp = /(?:^|[^{])\{([^{}]*)(?=\}(?!\}))/g; var match = myregexp.exec(subject); while (match != null) { for (var i = 0; i < match.length; i++) { // matched text: match[1] } match = myregexp.exec(subject); }

Answer2:

In many languages you can use lookaround assertions:

(?<!\{)\{([^}]+)\}(?!\})

Explanation:

    <li>(?<!\{): previous character is not a {</li> <li>\{([^}]+)\}: something inside curly braces, e.g. {foo}</li> <li>(?!\}): following character is not a }</li> </ul>

Recommend

  • Javascript Regex: Positive Lookbehind alternative
  • Negative Lookbehind JavaScript
  • mysql regex inverse (only for use by REGEXP)
  • Regex for matching literal strings
  • Regular Expression to match only one angle bracket
  • Catastophic backtracking issue with HTML [duplicate]
  • Regular expression to match single bracket pairs but not double bracket pairs
  • Combine/Merge Multiple Lines into One Line from a Text File (Powershell)
  • Boost split not traversing inside of parenthesis or braces
  • How to write a bison grammer for WDI?
  • trim phone number with regex
  • Merge strategy for whitespace only changes?
  • Draw Filled Shape from Four Points
  • How to make a DIV float on top of another DIV without pushing down the CSS Underneath
  • Python: remove double quotes from JSON dumps
  • SEO friendly 301 redirect .htm to .aspx
  • What is the first step to using a REST API in Rails?
  • overlapping appointments using the entity framework
  • Pytest/Allure - How to generate testcase description?
  • Is an if-let or a normal if condition better?
  • Paramiko SSHException Channel Closed
  • Does SmartGit support git-svn?
  • VB.Net Double comparison after some additions
  • Doctrine2 inverse persistance not working in nested forms
  • Zeromq with python hangs if connecting to invalid socket
  • custom string delimiters stringtemplate-4
  • matching similar elements in between two lists
  • SIP API media codecs
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • NUnit 3.0 TestCase const custom object arguments
  • Intel-64 and ia32 atomic operations acquire-release semantics and GCC 5+
  • What does 'Language neutral' mean with regard to MAKELANGID?
  • Android activity accessing service's static reference before the service is ready
  • Switching to Release Build causes runtime error in Web Reference
  • Resize panoramic image to fixed size
  • Transpose CSV data with awk (pivot transformation)
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal