-2

I'm trying to parse some strings where the values could be given like:

stuff "Something" stuff

or

stuff 'something' stuff

I solved this bit easily with:

(?<quote>"|')(?<quoted_value>[^'"]+)\k<quote>

However I've come across a problem where I have:

stuff "Today's Top 10" stuff

And I thought I could use the captured quote as the argument for the quoted_value pattern like so:

(?<quote>"|')(?<quoted_value>[^\k<quote>]+)\k<quote>

But I get

Unrecognized escape sequence \k 

How can I use the first quote capture value as a condition to capture everything until that value is found again?

4
  • What are you trying to do with \k? Commented Jun 15, 2020 at 7:59
  • @toto \k is Matches the value of a previously captured named group, specified by name. Commented Jun 15, 2020 at 8:01
  • Is this working for you? Commented Jun 15, 2020 at 8:04
  • @Toto OH, LEGEND! Yes it works perfectly. Didn't think of using a sub-pattern. Please add that as an answer so I can accept it. Commented Jun 15, 2020 at 8:08

1 Answer 1

1

You can use a Tempered Greedy Pattern:

(?<quote>['"])(?:(?!\k<quote>).)*\k<quote> 

Demo & explanation

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. Looks like a good resource too!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.