Regular Expression: Find and replace

Viewed 90

I need that every row contain 6 positions divided by “;” while in some rows it is like this: 1;2;3;4;6;3;5. So I need to unite last two digits to make it become like: 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

I have the following regex: \d;\d;\d;\d;\d;\d;\d\r which matches the string, how do I replace it with this one: \d;\d;\d;\d;\d;\d\d\r ? I’m using a text editor (Editpad pro and UltraEdit)

Thank you!

4 Answers

Try using capture groups:

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

When you want to replace string using regex, you have to use the capturing groups. You could try following regex.

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

Details:

  • (?:[0-9];){5}: string, that contains the number and semicolon, repeated 5 times. You find the string but do not get the string (?:).
  • ?<=: Positive Lookbehind
  • (\d);(\d): Group1, Group2 - gets the 6th, 7th position digit

Demo

You want to remove the sixth semi-colon in a semi-colon separated list of numbers.

Use

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

Replace with $1 (or \1 depending on your environment).

See proof.

Explanation

--------------------------------------------------------------------------------
  ^                        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
--------------------------------------------------------------------------------
  ;                        ';'
Related