Search code examples
c#.netregexlookbehind

Regex to match left double quote preceding with unclosed left double quote


Note :

left double quotation (") = &ldquo

right double quotation (") = &rdquo

My current regex is this

(?>=&ldquo.*)&ldquo

It matches the second &ldquo in here (which is right):

&ldquoThis is a sample&ldquo

It also matches the second &ldquo in here (which I don't want to happen because the 1st quote is already closed):

&ldquoThis is a sample&rdquoSample&ldquoTesting

How can I write a regex that will match every &ldquo preceding with the unclosed &ldquo

Thanks for all your help!


Solution

  • I think this is the answer:

    (?<=&ldquo.*)(?<!&rdquo.*)&ldquo
    

    Tested it there.

    Ah, it fails on &ldquoThis is a sample&rdquo &ldquoThis is a sample&ldquo


    My second attempt:

    (?<=&ldquo(?!.*&rdquo).*)&ldquo