Home > Net >  Remove regex Comma in notepad
Remove regex Comma in notepad

Time:10-27

I am trying to remove a comma that is located in a line of text. I need to remove a comma after the 7th comma.

I can use this ^[^,\n]*((,[^,\n]*){14}$) to locate the rows I want. So anything > 14 I need to remove the 7th comma.

Thanks in advance

  1. 1,2,3,4,5,6,7,8,9,10,11,12,13,14
  2. 1,2,3,4,5,6,7,,8,9,10,11,12,13,14,15
  3. 1,2,3,4,5,6,7,,8,9,10,11,12,13,14,15
  4. 1,2,3,4,5,6,7,,8,,9,10,11,12,13,14,15
  5. 1,2,3,4,5,6,7,8,9,10,11,12,13,14

CodePudding user response:

You can match the first 6 comma's, and then use \K to clear the match buffer.

Then match the 7th comma to be matched and replaced with an empty string, and assert at least 7 more comma's to the right afterwards.

^[^,\n]*(?:,[^,\n]*){6}\K,(?=[^,\n]*(?:,[^,\n]*){7})
  • ^ Start of string
  • [^,\n]*(?:,[^,\n]*){6} Match the first 6 comma's
  • \K, Forget what is matched so far, and match the 7th comma
  • (?=[^,\,]*(?:,[^,\n]*){7}) positive lookahead, assert 7 more comma's to the right

enter image description here

CodePudding user response:

To remove the 7th comma from lines containing 15 commas or more replace:

(?=(?:\w*,){15,})((?:,\w*){6}),

With

\1

Explanation:

  • (?=(?:\w*,){15,}) is a lookahead checking that the line contains 15 commas or more
  • ((?:,\w*){6}) is the first and only group of our regular expression. It captures the first six commas and the following word
  • , is the 7th comma you want to delete
  • \1 we replace the whole match with our captured group

Note: with this kind of expression you might delete the wrong comma in cases following this pattern:

1,2,3,4,5,6,7,8,9,10,,11,12,13,14,15
  • Related