Search code examples
regexregex-lookarounds

regex to remove last two letters in a number and word string


I'm trying to only match few ID's where there are either one or two alphabets at the end

Example:

ny12345d ---to be matched

ny12345dc -- to be matched

ny12346T01 --not to be matched

ny12346dfA --not to be matched

ny12345 ------not to be matched

so far I came up with this

/[^0-9]*$/g

Solution

  • You can use

    (?<![a-zA-Z])[a-zA-Z]{1,2}$
    

    See the regex demo

    Details:

    • (?<![a-zA-Z]) - a negative lookbehind that fails the match if there is an ASCII letter immediately to the left of the current location
    • [a-zA-Z]{1,2} - one or two ASCII letters at
    • $ - end of string.

    A capturing group approach:

    (?:[^a-zA-Z]|^)([a-zA-Z]{1,2})$
    

    See this regex demo. Here, (?:[^a-zA-Z]|^) is a non-capturing group matching either a char other than an ASCII letter or start of string, and ([a-zA-Z]{1,2})$ is similar to the pattern described above, with the letters captures into Group 1.

    NOTE: If you want to make sure there is a digit before the last one or two letters, you need

    (?<=\d)[a-zA-Z]{1,2}$
    (?<=[0-9])[a-zA-Z]{1,2}$
    \d([a-zA-Z]{1,2})$
    [0-9]([a-zA-Z]{1,2})$
    

    Here, \d or [0-9] matches any digit and (?<=\d) is a positive lookbehind that requires a digit to appear immediately to the left of the current location.