How to use regex to remove semi colons?

时间:2019-01-18 18:28:57

标签: regex notepad++ sublimetext3 sublimetext2

Hello I have this kind of data :

a;b;c;d
1;2;3;4
a;g;h;j
f;g;f;d
a;d;8;d

And I would like to modify to have this :

a;bc;d
1;23;4
a;gh;j
f;gf;d
a;d8;d

Obviously I have a lot of lines but every time the semi colons are in the same position. I tried to select the columns with notepad++ and to to replace the semi colon by nothing but the box is grey...

Do you have a solution ?

Thank you !

4 个答案:

答案 0 :(得分:0)

Hold ALT+SHIFT and use the arrow keys to select second semicolon and delete it.

OR

Hold ALT and click and drag the mouse to select a second semicolon and delete it.

OR

  • Find : ^(...)(.)
  • Replace with: \1

答案 1 :(得分:0)

Here is an online regex tester that i did the work on, you can just replace your data with the samples.

Regex : (\w+;)((\w+);(\w+))(;\w+)

DEMO

答案 2 :(得分:0)

  • Ctrl + H
  • 查找内容:^[^;]+;[^;]+\K;
  • 替换为:LEAVE EMPTY
  • 检查环绕
  • 检查正则表达式
  • 全部替换

说明:

^           # beginning of string
  [^;]+     # 1 or more non semicolon
  ;         # 1 semi colon
  [^;]+     # 1 or more non semicolon
  \K        # forget all we have seen until this position
  ;         # 1 semi colon

给定示例的结果

a;bc;d
1;23;4
a;gh;j
f;gf;d
a;d8;d

enter image description here

答案 3 :(得分:0)

如果数据的可能值为小写字母a-z或数字,则还可以使用捕获组和字符类[a-z0-9]捕获前三个字符,然后再匹配分号。如果可以包含多个字符,则可以对+

这样的字符类使用量词[a-z0-9]+

然后替换为第一个捕获组。

查找内容

^([a-z0-9];[a-z0-9]);

替换为

$1

Regex demo

或者使用\K可以找到^[a-z0-9];[a-z0-9]\K;并将“替换为”保留为空。