Mohit Kumar Jangid
Mohit Kumar Jangid

Reputation: 109

Regex: Match a pattern within quoted texts

Text is

lemma A:
"
 abx K() bc
"

// comment lemma B
lemma B:
"
 abx bc sdsf
"


lemma C:
"
 abfdfx K() bc
"


lemma D:
"
 abxsf bc
"

I want to find the lemmas which contain K() inside its following quoted text. I have tried Perl regex (?s)^[ ]*lemma.*?"(?!").*?K\( but it overlaps two lemmas. The output should be: lemma A: "..." and lemma C: "...".

Upvotes: 2

Views: 52

Answers (2)

The fourth bird
The fourth bird

Reputation: 163277

If the double quotes are at the start of the string, you can match a newline and then the double quote.

Then match any char except the double quote until you match K(

^[ ]*lemma\b.*\R"[^"]*K\(
  • ^ Start of string
  • [ ]*lemma\b Match optional spaces and lemma
  • .*\R Match the rest of the line and a newline
  • "[^"]* Match " followed by optional chars other than "
  • K\( Match K(

Regex demo

Upvotes: 1

logi-kal
logi-kal

Reputation: 7880

You could use:

(?s)^[ ]*lemma[^"]*"[^"]*?K\(

[^"] means "any character but ""

See a demo here

Upvotes: 0

Related Questions