Aron Woost
Aron Woost

Reputation: 20668

Find strings in strings

I have the following string:

here "is 'a' \"string\" that" does contain a 'lot of "weird"' stuff "i" to 'find'

I want to extract is 'a' \"string\" that, lot of "weird", i and find.

Any ideas? Thanks!

Upvotes: 3

Views: 83

Answers (2)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

You can try with this pattern:

/"((?:[^"\\]+|\\"|\\)*)"|'((?:[^'\\]+|\\'|\\)*)'/g

Content inside double quote is in group 1, content inside single quote is in group 2.

Notice: this solution is not totally waterproof for escaped quotes because if \", that can detect the pattern above, is preceded by another \, then the \\ is seen as a literal \ and the quote is no more escaped!

To avoid this trap, you can check cases when you have an odd number of backslashes replacing \\" by (?:\\{2})*\\", then the first pattern will look like this:

/"((?:[^"\\]+|(?:\\{2})*\\"|\\)*)"|'((?:[^'\\]+|(?:\\{2})*\\'|\\)*)'/g

Upvotes: 2

anubhava
anubhava

Reputation: 785176

This negative lookbehind based regex should work for you:

/(["']).*?(?<!\\)\1/g

Live Demo: http://www.rubular.com/r/X3etmgKFNH

Regex without support of lookbehind e.g. Javascript

/(['"])(?:\1|.*?[^\\]\1)/g

Live Demo: http://www.rubular.com/r/SQyEYIPS4l

Upvotes: 2

Related Questions