-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?

Phill
  • 18,398
  • 7
  • 62
  • 102

1 Answers1

1

You can use a Tempered Greedy Pattern:

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

Demo & explanation

Toto
  • 89,455
  • 62
  • 89
  • 125