jlo
jlo

Reputation: 2269

Select the second instance of a string under quotation marks with regex

How can I select the second string under quotation marks with regular expressions?

For example:

entry = ("plk", "Kopiuj - linearnie");
entry = ("ptb", "Copiar - linear");

I know that "[^"]*" selects everything under quotation marks, but what I'd like to find with regex is "Kopiuj - linearnie" and "Copiar - linear" and ignore the first string under " like "plk" and "ptb" (i.e. the second string per line).

Cheers,

Upvotes: 0

Views: 132

Answers (3)

mouckatron
mouckatron

Reputation: 1289

If you do

/"([^"]*)"[^"]*"([^"]*)"/

then the first string will be in the variable $1 and the second in $2 if you use a regex replace function.

Or you can do

/"[^"]*"$/

which will match at the end of the line (the $ character), so it only matches the last text.

http://www.regextester.com/index2.html is great for testing your regular expressions.

Upvotes: 0

Barmar
Barmar

Reputation: 781726

"[^"]*", *"([^"]*)"

Then access the first capture group.

Upvotes: 1

yuri kilochek
yuri kilochek

Reputation: 13484

Just make it capture the closing parenthesis, like so

"([^"]*)"\)

the resulting string is in the first capture group.

Upvotes: 2

Related Questions