正则表达式:查找并替换

时间:2020-09-13 09:01:37

标签: regex

我需要每行包含6个位置,然后除以“;”而在某些行中则是这样的:1; 2; 3; 4; 6; 3; 5。所以我需要将最后两位数字统一起来,使其变为:1; 2; 3; 4; 6; 35

1;2;3;4;6;7
1;2;3;4;6;8
1;2;3;4;6;9
1;2;3;4;6;10
1;2;3;4;6;11
1;2;3;4;6;12
1;2;3;4;6;13
1;2;3;4;6;14
1;2;3;4;6;15
1;2;3;4;6;16
1;2;3;4;6;17
1;2;3;4;6;18
1;2;3;4;6;19
1;2;3;4;6;20
1;2;3;4;6;21
1;2;3;4;6;22
1;2;3;4;6;23
1;2;3;4;6;24
1;2;3;4;6;25
1;2;3;4;6;26
1;2;3;4;6;27
1;2;3;4;6;28
1;2;3;4;6;29
1;2;3;4;6;30
1;2;3;4;6;31
1;2;3;4;6;32
1;2;3;4;6;33
1;2;3;4;6;34
1;2;3;4;6;3;5
1;2;3;4;6;3;6
1;2;3;4;6;3;7
1;2;3;4;6;3;8
1;2;3;4;6;3;9
1;2;3;4;6;4;0
1;2;3;4;6;4;1
1;2;3;4;6;4;2
1;2;3;4;6;4;3
1;2;3;4;6;4;4
1;2;3;4;6;4;5
1;2;3;4;6;4;6
1;2;3;4;6;4;7
1;2;3;4;6;4;8
1;2;3;4;6;4;9
1;2;3;4;6;5;0
1;2;3;4;6;5;1
1;2;3;4;6;5;2
1;2;3;4;6;5;3
1;2;3;4;6;5;4
1;2;3;4;6;5;5
1;2;3;4;6;5;6
1;2;3;4;6;5;7
1;2;3;4;6;5;8
1;2;3;4;6;5;9
1;2;3;4;6;6;0
1;2;3;4;6;6;1
1;2;3;4;6;6;2
1;2;3;4;6;6;3
1;2;3;4;6;6;4
1;2;3;4;6;6;5
1;2;3;4;6;6;6
1;2;3;4;6;6;7
1;2;3;4;6;6;8
1;2;3;4;6;6;9
1;2;3;4;6;7;0
1;2;3;4;6;7;1
1;2;3;4;6;7;2
1;2;3;4;6;7;3
1;2;3;4;6;7;4
1;2;3;4;6;7;5

我有以下正则表达式:\d;\d;\d;\d;\d;\d;\d\r与字符串匹配,如何用此字符串替换它:\d;\d;\d;\d;\d;\d\d\r ? 我正在使用文本编辑器(Editpad Pro和UltraEdit)

谢谢!

4 个答案:

答案 0 :(得分:1)

您要删除以分号分隔的数字列表中的第六个分号。

使用

^(\d+(?:;\d+){5});

替换为$1(或\1,具体取决于您的环境)。

请参见proof

说明

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (5 times):
--------------------------------------------------------------------------------
      ;                        ';'
--------------------------------------------------------------------------------
      \d+                      digits (0-9) (1 or more times
                               (matching the most amount possible))
--------------------------------------------------------------------------------
    ){5}                     end of grouping
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  ;                        ';'

答案 1 :(得分:0)

尝试使用捕获组:

Find:    (\d;\d;\d;\d;\d;)(\d);(\d)\r?\n
Replace: $1$2$3\n

答案 2 :(得分:0)

您可以使用

^((?:[^;\n]+;){5})([^;\n]+);(.+)$

并替换为

$1$2$3

请参见a demo on regex101.com

答案 3 :(得分:0)

当您要使用正则表达式替换字符串时,必须使用捕获组。 您可以尝试遵循正则表达式。

(?<=(?:[0-9];){5})(\d);(\d)

详细信息

  • (?:[0-9];){5}:包含数字和分号的字符串,重复5次。您找到了字符串,但未获得字符串?:)。
  • ?<=:积极回望
  • (\d);(\d):Group1,Group2-获取第6、7位数字

Demo