regexregex-lookaroundsnegative-lookbehind

Regex to match 0 or an even number of consecutive characters?


I'm looking to see if it's possible to have a regex pattern that matches content between enclosing single quotes in a string, with the requirement that single quotes are escaped using another single quote.

Example:

The quick 'brown fox ''jumped ''''over the lazy dog''';

The regex should capture this string: 'brown fox ''jumped ''''over the lazy dog'''. Since a single quote is escaped using another single quote here, the rest of the string isn't included.

This is what I have so far (?<!\')\'(?!\').+(?<!\')\'(?!\')

This almost works, except the group isn't captured if the closing non-escaped single quote has an escaped quotes before it.

Is it possible to change the negative lookbehind to say that a single quote should be matched if there are either 0 or an EVEN number of single quotes behind it?


Solution

  • You may use this regex without any look around:

    '((?:[^']+|'')*)'
    

    RegEx Demo

    RegEx Breakup: