Search code examples
regexnotepad++

Regex pattern to search multiple word occurrences and capture the second matched only


I want to capture the second occurrence using REGEX pattern in notepad++ which is "Florida; West Virginia; DC only."

 Notepad++
 1 Arkansas; Hawaii; South Dakota; North Carolina
 2 California;Florida; Washington State; New York; Florida; West Virginia; DC
 3 Nevada; Texas; New Mexico; Georgia   


Regex Pattern: '\Florida(.*?)\DC'

Actual Capture: Florida; Washington State; New York; Florida; West Virginia; DC
Desired Capture: Florida; West Virginia; DC  

Solution

  • You can use a negative lookahead asserting no more occurrences of Florida to the right.

    For a match only, you don't need the capture group. Also the \F and \D do not have to be escaped.

    \bFlorida\b(?!.*\bFlorida\b).*
    

    Regex demo

    Or optionally match the first occurrence, and capture from the second occurrence on in a capture group.

    ^(?:.*?\bFlorida\b)?.*?\b(Florida\b.*)
    

    Regex demo