Search code examples
regexnotepad++

Regular Expression, 4th parameter


I have some code, there is 7 parameters in each line. Each line is in parentheses and is ended with a semicolon. I need to take 2000 away from the 4th parameter of each line. Basically all I need to do is take off the first digit (2) off the beginning of the 4th parameter. How can I do this? Also, try to explain how it works please, trying to learn how to use regex properly.

Each line is like this: (689,746.37311,1064.86426,2518.65820,0.00000,0.00000,0.00000);

Als0, every line's 4th parameter is two-thousand something.


Solution

  • You can use this:

    ^(?:[^,]*,){3}\K\d
    

    details:

    ^           # anchor for the start of the line
    (?:         # open a non-capturing group
        [^,]*   # characters that are not a comma (reach the next ,)
        ,       # a comma
    ){3}        # close the group and repeat it three times
    \K          # remove all on the left from the match result
    \d          # the digit
    

    The general idea of the pattern is to reach the third comma from the start of the line, and to take the digit immediately after.