Search code examples
regexpositive-lookbehind

Multiple regex lookbehind


I am searching for a string which has several occurrences of other string/char before it:

this is not real example, but it describes what I want to capture - string that occurs right after second (or third, the expression must be scalable and configurable) occurrence of the ":" character.

boo+foo:qoo+loo:thisIsWhatIneed...

Obviously, the following did not work:

(?<=\:){2,}.*

and this would be pretty much opposite (inverted) of what I want:

.*:.*:

enter image description here


Solution

  • This will match what you needed from your example:

    [^:]*:[^:]*:(.*)

    ignored:ignored:captured as group 1

    This will let you specify the number of colon delimited words to ignore more easily:

    (?:[^:]*:){2}(.*)

    This will capture everything after the last colon until the end, regardless of how many colons there are:

    :([^:]*)$

    regex test website showing results