0

I need help with a regex for matching a quoted string that could have an embedded escaped quote.

For example, given string "'John\'s bike'". The basic pattern i was starting with, "'[^']*'", and expanding it to negate case of embedded escaped quotes using lookbehind doesn't work: "'((?<\\)[^'])*'".

Anyone has idea?

The string could be a series of quoted strings, e.g., "'John\'s bike', 'Mary\'s hat', 'my shoes'", and i want to tokenize each of the quoted substrings separately.

2
  • You have not demonstrated to us what your problem is sufficiently. Why can't you just use "'.*'" which captures your single test case. Demo here Do you not wish to capture `John\'s bike'? Commented Nov 11, 2015 at 22:36
  • Once you add the double quote as part of the regex "' .. '" you introduce a double delimiter. Not really too good. By double delim, I mean a delimiter that consists of 2 characters, that have open/close specific context. Like should "'hello\'" world match? Commented Nov 11, 2015 at 23:55

1 Answer 1

2

You can accept any character between quotes which is

  • preceded by \
  • not '.

So your regex can look like:

'(\\.|[^'])*'

which in Java should look like "'(\\\\.|[^'])*'" (to create \ literal in regex we need to escape it like \\ but same rules applies in String literals so we need to escape these two \ again which finally gives us "\\\\")

Demo based on your example:

String input = "'John\\'s bike', 'Mary\\'s hat', 'my shoes'";
Pattern p = Pattern.compile("'(\\\\.|[^'])*'");
Matcher m = p.matcher(input);
while(m.find()){
    System.out.println(m.group());
}

Output:

'John\'s bike'
'Mary\'s hat'
'my shoes'
Sign up to request clarification or add additional context in comments.

8 Comments

Why didn't i think of that? I was making it more complicated by explicitly looking for escaped backlash. Thank you!
this fails for \\". The regex "(\\\\[^\\\\]|[^\"])*" should do the trick.
@mvallebr What do you mean by fails? What \\" represents? IMO it represents escaped \ and not escaped " in which case we can consider that " as opening\closing quote (I am assuming that in your case " is quotation symbol, not ').
The original question said nothing about escaping back slashes. I never thought of it as a requirement. The requirement, for me, was solely escape quotes, nothing more.
So how exactly should foo\ be placed at the end of quote? If we write is as "...foo\" it will represent foo and escaped " so \ disappears (which is NOT what we want).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.