2

How do I remove the last column from a comma delimited file in notepad++ using regex with replace and find?

This is my file:

2010-09-02,148.67,31.34,0,0,31.34
2010-09-03,149.52,31.24,0,0,31.24
...

I am trying to find this pattern at the end of each line, basically the last column or everything after the last comma, for example:

,31.34
,31.34

This is what I have so far, but it seem to select the whole line:

.*,\d+.\d+

I just want to select ,31.34 and remove it, that is replace it with nothing.

4

I tested the following regex, and it works:

Search for: (.+),.+?$
Replace with: \1

The regex explained:

(.+),.+?$
(  )      = this is a capture group. Its content will be \1, \2, \3 in order of appearance.
 .+       = This selects everything greedy mode
    ,     = until it finds the last comma.
(.+),     = get everyting except the last , and store it in \1.
     .+?  = Get everything, non greedy.
        $ = until we find the end of the string.

Unlike the regex in your question, this will work regarding the content, as long as its all
separated by a ,.

  • tks, can you just explain what the \1 is compared with just using "" ? – HattrickNZ Sep 5 '16 at 20:58
  • 1
    "" means removing. But what my regex does, is finding everything upto that last , and store it in \1, and makes sure it searches for the entire row. So when you replace, the entire row gets replaced with whatever you want. Using \1 means, the first part all the way up to the last ,. – LPChip Sep 6 '16 at 12:02
  • 1
    If I had a string that says: test and I searched for (t)(e)(s)(t), then it would select everything, and I can replace it by \1\2\3\4 to get test back, or \1\2\4 to get tet, or \2\3\1\4 to get estt – LPChip Sep 6 '16 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.