alexko
alexko

Reputation: 65

Regex replace one value between comma separated values

I'm having a bunch of comma separated CSV files. I would like to replace exact one value which is between the third and fourth comma. I would love to do this with Notepad++ 'Find in Files' and Replace functionality which could use RegEx.

Each line in the files look like this:

03/11/2016,07:44:09,327575757,1,5434543,...

The value I would like to replace in each line is always the number 1 to another one.

It can't be a simple regex for e.g. ,1, as this could be somewhere else in the line, so it must be the one after the third and before the fourth comma...

Could anyone help me with the RegEx? Thanks in advance!

Two more rows as example:

01/25/2016,15:22:55,276575950,1,103116561,10.111.0.111,ngd.itemversions,0.401,0.058,W10,0.052,143783065,,... 01/25/2016,15:23:07,276581704,1,126731239,10.111.0.111,ll.browse,7.133,1.589,W272,3.191,113273232,,...

Upvotes: 1

Views: 2830

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626691

You can use

^(?:[^,\n]*,){2}[^,\n]*\K,1,

Replace with any value you need.

The pattern explanation:

  • ^ - start of a line
  • (?:[^,\n]*,){2} - 2 sequences of
    • [^,\n]* - zero or more characters other than , and \n (matched with the negated character class [^,\n]) followed with
    • , - a literal comma
  • [^,\n]* - zero or more characters other than , and \n
  • \K - an operator that forces the regex engine to discard the whole text matched so far with the regex pattern
  • ,1, - what we get in the match.

enter image description here

Note that \n inside the negated character classes will prevent overflowing to the next lines in the document.

Upvotes: 2

user2705585
user2705585

Reputation:

You can replace value between third and fourth comma using following regex.

Regex: ([^,]+,[^,]+,[^,]+),([^,]+)

Replacement to do: Replace with \1,value. I used XX for demo.

Regex101 Demo

Notepad++ Demo

Notepad++ Demo.

Upvotes: 0

Related Questions