2

I have this regex: (?:\[|\G(?!^))('[^']+?')\s*,?\s*(?=[^\]]*?\]) which matches only content between quotes inside square brackets (like an array): ['Foo', 'Bar'] => returns 'Foo' 'Bar'.

The problem is that in this case the single quote is an special character, since it's used by the regex as a delimiter, but it's necessary for me to pass sometimes the single quote inside the value as a escaped character: ['F'oo', 'B'ar'].

I'm trying to do something like 'F\'oo', by adapting this non-capturing (?:(?=(\\?))\1.) group to the regex, but it doesn't work, I tried many different ways.

This non-capturing group regex comes from this answer, where the he successfully uses the backslash to escape special characters.

I use C# with .NET Core.

The full text is something like: eq('Property', ['F'oo', 'Ba'r', '123'])

How can this be solved?

MurariAlex
  • 321
  • 1
  • 3
  • 20
  • Can't just use `'.+?'` to wash out delimiters, doesn't work that way. Petrod's `['F'o''o',',',',',',','ar'].` –  Oct 10 '19 at 20:38

3 Answers3

2

There is a similar question already for getting quoted escaped characters. I'd prefer this answer.

Change your capturing part ('[^']+?') to ('[^\\']*(?:\\.[^\\']*)*'). You can further drop the lazy quantifier which won't make much difference when using a negated class already.

It might be necessary to do additional escaping of the backslash.

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
  • Your solution is great! Does exactly what I asked! The only thing that differed from the accepted answer is the need for the backslash. Thank you! – MurariAlex Oct 10 '19 at 12:10
  • @MurariAlex Thanks :) Yes that's how I understood the question. To deal with backslash escaped single quotes inside the single quoted string. Good you found a solution that fits your needs! – bobble bubble Oct 10 '19 at 12:56
2

You could maybe use

(?:\[|\G(?!\A))
('.+?')
(?:(?:\s*,\s*)|\])

See a demo on regex101.com.

Jan
  • 42,290
  • 8
  • 54
  • 79
  • Excelent solution! Not only you kept the basic premise that was only match items inside the square brackets, you went one step further and gave a solution that spared the use of the backslash to escape the quote. Thanks! – MurariAlex Oct 10 '19 at 11:57
0

My guess is that maybe,

(?<=\[|,)\s*'(.*?)'\s*(?=\]|,)

or some expression similar to that might work OK.

Demo

Test

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"(?<=\[|,)\s*'(.*?)'\s*(?=\]|,)";
        string input = @"['Foo', 'Bar']
['F'oo', 'B'ar']
['F'oo', 'B'ar','Foo', 'Bar']";
        RegexOptions options = RegexOptions.Multiline;
        
        foreach (Match m in Regex.Matches(input, pattern, options))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }
    }
}

RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

Community
  • 1
  • 1
Emma
  • 27,428
  • 11
  • 44
  • 69
  • 1
    Thanks for your answer! It is some excellent solution too! The only thing that is missing that I needed is to only match the elements when the square brackets are present (in the full text), so I can enforce syntax. Other than that, you nailed it! Thank you! – MurariAlex Oct 10 '19 at 12:05