regextextreplacenotepad++timetable

Modify raw input to look like a bus timetable in specific format using regex?


I'm trying to figure this out for quite some time already, but can't seem to find the solution that would work at once or in the way I prefer it.

I have an input that looks like this:

0430
0500 25 50
0615 34 51
0708 26 43

And I need to turn it into this:

04:30
05:00,05:25,05:50
06:15,06:34,06:51
07:08,07:26,07:43

Since this is only part of the input and manually replacing everything isn't an option, my guess is that the best option is to go with regex.

What needs to be done:

  1. Insert colon after the first two ciphers (something like (^\d{2}) and then doing replace/substitution with $1:)
  2. Replace each space with comma + first two ciphers + colon. My idea was to capture group (^\d{2}:) and then replace all spaces with ,$1 (per each line), but I can't seem to find the way to do it.

I use regex101.com for doing it, so if you have any advice on how to do it, or where to do it (or even if regex isn't the way to do it, what other way would you recommend) any help would be appreciated.

Thanks in advance!


Solution

  • Here is a way to do the job with Notepad++:

    Explanation:

    ^               # beginning of line
      (\d\d)        # group 1, 2 digits
      (\d\d)        # group 2, 2 digits
      (?:           # non capture group
        \h+         # 1 or more horizontal spaces
        (\d\d)      # group 3, 2 digits
        \h+         # 1 or more horizontal spaces
        (\d\d)      # group 4, 2 digits
      )?            # end group, optional
    

    Replacement:

    $1              # content of group 1
    $2              # content of group 2
    (?3             # if group 3 exists
      ,$1\:$3           # a comma then content of group 1 and 3
      ,$1\:$4           # a comma then content of group 1 and 4
      :             # else nothing
    )               # end conditional
    

    Screen capture (before):

    enter image description here

    Screen capture (after):

    enter image description here