use ultraedit find and replace Perl regex to insert colon into 4 digit time string

92 Views Asked by At

I have multiple 24-hour time strings through several files. For example, 1234, which I wish to replace with 12:34.

Finding them is easy, just \d\d\d\d, that I understand and it works. However, what replace string do I need. In other words, say xx:xx, what do I put in place of each x.

I've tried numbers of things to no avail. I'm obviously not understanding how I get it to remember the digits it found and to recall them in the replace string.

1

There are 1 best solutions below

0
The fourth bird On BEST ANSWER

If in your example data 4 digits represent 24 hour time strings you could match 2 capturing groups between word boundaries to prevent a match with more then 4 digits. You can Adjust the word boundaries to your requirements.

Match

\b(\d{2})(\d{2})\b

Replace

group1:group2 \1:\2

Explanation

  • \b Match a word boundary
  • (\d{2}) Capture in a group 2 digits
  • (\d{2}) Capture in a group 2 digits
  • \b Match a word boundary

Note

Matching 4 digits does not verify a valid 24 hour time. You could match that using for example \b([01][0-9]|2[0-3])([0-5][0-9])\b and replace with \1:\2