regexreplaceselection

Unable to write a regular expression, no AI I've tried works


I need a PCRE Regular Expression that will find and select a specified phrase at the beginning of sentences, only if it is preceded by a sentence that also contains the phrase. The first sentence containing the phrase should not be selected.

Example:

Phrase A: Words
Phrase A: Words
Phrase A: Words
Phrase B: Words
Phrase B: Words
Phrase A: Words
Phrase A: Words
Phrase B: Words
Phrase A: Words
Phrase A: Words

In the above sequence, the user specifies Phrase A: in the expression, and it should then select only Phrase A: in lines 2, 3, 7, and 10:

Phrase A: Words
> Phrase A: Words
> Phrase A: Words
Phrase B: Words
Phrase B: Words
Phrase A: Words
> Phrase A: Words
Phrase B: Words
Phrase A: Words
> Phrase A: Words

I absolutely suck at writing regular expressions. I've tried every AI I have access to, including DeepSeek v3, ChatGPT o3-mini high, and Claude Sonnet 3.7 extended. They all produce expressions that either don't work or have errors.

I've spent three hours on this so far. :(


Solution

  • Try this:

    (?<=Phrase A)[\s\S]*?(Phrase A)