Search code examples
regexnotepad++regex-lookarounds

Regex positive look behind notepad++


I'm trying to use regex to grab the last 3 digits of a string that varies in size. The beginning of the string always leads with name- Examples:

"Name-0000425" "Name-00123" "Name-123"

I want the regex to grab the last 3 digits of this number and replace it with these digits. This is the regex I have tried but it only grabs the first 3 digits.

(?-s)(?<=Name-)(\d{3})

Any help would be appreciated! Thanks!


Solution

  • You can use

    (?<=Name-)\d*(\d{3})
    

    Replace with $1. See the regex demo.

    Alternatively, you may also use (Name-)\d*(\d{3}) and replace with $1$2.

    Details

    • (?<=Name-) - a positive lookbehind that matches a location immediately preceded with Name-
    • \d* - any zero or more digits
    • (\d{3}) - Group 1 ($1 refers to this value from the replacement pattern): three digits.

    NOTES

    • If you only want to remove initial zeros, you can use the (?<=Name-)0+(\d{3}) regex.
    • If there can be anything else before the last three digits on the line, you may use (?<=Name-).*(\d{3}).
    • If there can be anything else before the last three digits that are inside double quotes on a single line, you may use (?<=Name-)[^"\n\r]*(\d{3}).