Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Search and replace between digits with regular expression

I want to search with Notepad++ in a subtitle file for "," and ut should be replaced with "." between the digits of the timestamps but not in the rest of the text strings.

Source:

00:01:17,000 --> 00:01:20,000
vor allem aber an Ausläufen, Reitplätzen
und Weideflächen.

At the moment I’m searching with: "," and for the replacing I’m using the regex "\1.\2". But this will change also all other commas between the text strings.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • Search-Pattern: [,]
  • Replace-Pattern: [\1.\2]

Result:

00:01:17.000 --> 00:01:20.000
vor allem aber an Ausläufen. Reitplätzen
und Weideflächen.

How I have to expand the regular expression to replace the commas only between the digits of the timestamps?

What I expect:

00:01:17.000 --> 00:01:20.000
vor allem aber an Ausläufen, Reitplätzen
und Weideflächen.

>Solution :

You could use a bit more specific regex for Notepad++

\b\d{2}:\d{2}:\d{2}\K,(?=\d{3}\b)

The pattern matches:

  • \b A word boundar to prevent a partial word match
  • \d{2}:\d{2}:\d{2} Match the hours, minutes and seconds part
  • \K, Forget what is matched so far and then match a comma
  • (?=\d{3}\b) Positive lookahead, assert 3 digits followed by a word boundary to the right

In the replacement use a dot .

See a regex demo.


Or use 2 capture groups and use those groups in the replacement separated by a dot $1.$2

\b(\d{2}:\d{2}:\d{2}),(\d{3})\b

See another regex demo.


A fully matching pattern with 3 capture groups, where \h+ matches 1 or more horizontal whitespace characters:

\b(\d{2}:\d{2}:\d{2}),(\d{3}\h+-->\h+\d{2}:\d{2}:\d{2}),(\d{3})\b

See another regex demo

In the replacement use $1.$2.$3

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading