Search code examples
regexpattern-matchingpositive-lookahead

Pull last 3 charecters from Regex positive lookhead match


I have the following regex expression ^.*?(?=\.) and i'm looking to modify it so that i can pull the 3 charecter country code (bold) from a given path. Right now the regex just creates a group before the period.

/path/to/folder/Conclusions_Pakistan_2019-09-13_PAK.pdf

regex101 link: https://regex101.com/r/FGjqSl/1


Solution

  • Your regex ^.*?(?=\.) matches everything from start until it asserts presence of . hence it matches too much.

    You may just use this regex 3 character country code comprising all upper case letters:

    [A-Z]{3}(?=\.)
    

    Updated Regex Demo