Search code examples
regexnotepad++

Find pattern HH:MM in Notepad++ with a regular expression


I'm using the latest version of Notepad++.

I have a text file and I want to delete all the characters except this one: (14:32).

The text if a list of lessons in a online course and I want to extract the time to sum all of them because I want to know how long it is.This is an example of the text:

Lecture 1.
    Programming robots (15:32)
    Understanding ROS (8:20)

I want to remove everything to get something like this:

(15:32)
(8:20)

I have tried these regular expressions:

^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$

/^(0?[1-9]|1[0-2]):[0-5][0-9]$/

But I can't find anything with them.

How can I find this pattern in Notepad++?

Maybe, it could be easier find all the characters ([a-z][A-Z]) and replace them with nothing.


Solution

  • You can use

    Find What: ^.*(\((?:[01]?[0-9]|2[0-3]):[0-5][0-9]\)).*
    Replace With: $1

    enter image description here

    See the regex demo.

    Details:

    • ^ - start of string
    • .* - any zero or more chars other than line break chars as many as possible
    • (\((?:[01]?[0-9]|2[0-3]):[0-5][0-9]\)) - Group 1:
      • \( - a ( char
      • (?:[01]?[0-9]|2[0-3]) - an optional 0 or 1 digit, and then any one digit, or 2 and then a digit from 0 to 3 range
      • : - a : char
      • [0-5][0-9] - minutes part (a digit from 0 to 5 and then any one digit)
      • \) - a ) char
    • .* - the rest of the line.