Search code examples
regexnotepad++

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.

  • 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 boundary 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