Search code examples
regexregex-lookaroundscapturing-group

Advance a regex forward one character


I'm looking for expressions between a period and a semicolon - this expression cannot itself contain periods or semicolons.

Here is my regex so far.

(?!\..*?\..*?;)\..*?;

The regex basically says "Find string between a period and a semicolon. However, upon identifying a candidate period, if it is eventually followed by another period before reaching the semicolon, then I want to disqualify it."

This does what I want, except I don't want to include the initial period in the match (nor the terminal semicolon). I tried to move forward with a non-capturing group (?!\..*?\..*?;)(?:\.).*?; but I get the same result. How can I omit the period (and semicolon) as part of the match? I'm using Sublime 3 to search for regular expressions.


Solution

  • It sounds like you might want to use a negative character set instead, which will allow you to match any character but the ones you put in the set (exclude periods and semicolons). Then, use lookahead to check that the non-period characters are followed by a semicolon, without actually matching the semicolon, and use lookbehind to ensure that the pattern is immediately preceded by a period:

    So, the pattern

    (?<=\.)[^.;]+(?=;)
    

    will match non-period, non-semicolon characters which are after a period and followed by a semicolon.

    https://regex101.com/r/E8XsDB/2