如何删除列CSV Notepad ++ / RegEx?

时间:2016-08-31 16:42:06

标签: regex csv notepad++

a,b,c,d
aaa,2.15,100.15,-
bbb,3.16,215.16,-
ccc,4.10,365.18,-

我想删除

2.15,
3.16,
4.10,

,-
,-
,-

我想删除列b + d

img1

img2

,[\ W]。* [\ W]。

3 个答案:

答案 0 :(得分:1)

Just capture everything but the two columns.
(Edit: added \r\n so don't span lines)

X = Before col 1: = b-1
Y = Before col 2: = d-1 - X - 1

Find:
(?m)^((?:[^,\r\n]*,){X})[^,\r\n]*,((?:[^,\r\n]*,){Y})[^,\r\n]*(.*)

Replace:
$1$2$3

Example -

X = 2-1 (Col 2) = 1
Y = 4-1 - X - 1 (Col 4) = 1

Regex: (?m)^((?:[^,\r\n]*,){1})[^,\r\n]*,((?:[^,\r\n]*,){1})[^,\r\n]*(.*)


Or, if this is a 1-off thing, use

(?m)^([^,\r\n]*,)[^,\r\n]*,([^,\r\n]*).*

Replace $1$2

Which just eliminates column's 2 and 4.

答案 1 :(得分:1)

Notepad++ solution:

  • Find what: ^(.*?),.*?,(.*?),.*$
  • Replace with : $1,$2
  • Search mode : Regular expression (without the . matches newline)

The regex will put the first and 3rd column into capture groups.
Which are used in the replace.

答案 2 :(得分:0)

Just search for this:

(\w+),\d+\.?\d*,(\d+\.?\d*),-

And replace with this:

$1,$2

End result:

a,b,c,d
aaa,100.15
bbb,215.16
ccc,365.18

It supports numbers with or without decimals, any number of digits. You may have to modify it if the first column has special characters.