dNitro
dNitro

Reputation: 5355

Regex to match last single quote that is really single

What is regex to match the last single quote in a string that is really single ( i mean it does not have any partner back) so we should get last quote in the:

foo('bar').baz("foo'foo").bar('baz').foo('bar

but nothing to match in the following:

foo('bar').baz

Upvotes: 0

Views: 65

Answers (1)

SamWhan
SamWhan

Reputation: 8332

I believe this is quite a complex task, especially since you're involving double quotes. But you might make it work with something like this:

^('[^']*'|"[^"]*"|[^'"])*$

From the start of the string, it matches either

  • a single quoted string
  • a double quoted string
  • or a character not being a single or double quote

Then it goes on 'til the end of the string. If it encounters unbalanced quotes, it fails.

I'm pretty sure this will fail in many cases, but the simplest ones should work.

Check it out at regex101.

Edit: In the example I had to put newline - \n - in negated classes as well to illustrate several examples.

Upvotes: 1

Related Questions