0

How to match a closing quote (single or double) depending on opening quote used?

For now I have:

key.*?=["'].*?["']

But unfortunately the closing quote is not dependent on the opening one and it won't work for the following example:

key="foo'bar"

I could created 2 sub but I assume that it isn't optimal:

key.*?=(".*?"|'.*?')

Any hint would be greatly appreciated, thanks!

1
  • Have you tried capturing the first quote in a group and using a back-reference at the end? Commented May 15, 2016 at 8:26

1 Answer 1

1

You can use back reference \n (e.g. \1 - for the first capturing group, \2 for the second)

So your example would look like:

key.*?=(["']).*?\1
Sign up to request clarification or add additional context in comments.

Comments

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.