How to change timestamp to hh:mm:ss.mss format



  • Hi I have lines in a document that has this format: Thh:mm:ssZ

    I need to change it to Thh:mm:ss.mssZ

    How do I search/replace in this format using regex? Thank you.



  • Are you just going to invent data for the missing mss ? Or do you simply want it to be always 000?



  • Hi Alan, I just want it to be 000Z



  • @Elijah-Alcarde ,

    Then assuming it’s always two digits for every hour, minute, and second element,

    • find = T(\d\d:\d\d:\d\d)Z
    • replace = T${1}.000Z
    • search mode = regular expression

    should work for you.

    If some of the hours are only single-digit hours, (ie, don’t have the leading zero),

    • find = T(\d{1,2}:\d\d:\d\d)Z


  • @PeterJones Thank you so much it worked!

    It would be much appreciated if you explain a little bit how you did the replace :)



  • For further info, see my boilerplate below.

    For specifics of this, I’ll give a brief description:

    The T and Z are literals, in both the search and replace expressions. The (...) makes a group#1 which contains whatever matches inside. \d matches any digit (0-9, and some other unicode “digit” characters from other languages). \d\d thus matches two consecutive digits; \d{1,2} matches 1 or 2 digits. : matches the literal :. Thus, T(\d\d:\d\d:\d\d)Z matches the hh:mm:ss in between a T and a Z, and stores it in group#1. For the replace, ${1} is the contents of group#1, and everything else is a literal so, "replace it with a T, followed by the contents of group#1 from the find-match, followed by .000 and finally a Z".

    -----

    Please Read And Understand This

    FYI: I often add this to my response in regex threads, unless I am sure the original poster has seen it before. Here is some helpful information for finding out more about regular expressions, and for formatting posts in this forum (especially quoting data) so that we can fully understand what you’re trying to ask:

    If you have further search-and-replace (“matching”, “marking”, “bookmarking”, regular expression, “regex”) needs, study the official Notepad++ searching using regular-expressions docs, as well as this forum’s FAQ and the documentation it points to. Before asking a new regex question, understand that for future requests, many of us will expect you to show what data you have (exactly), what data you want (exactly), what regex you already tried (to show that you’re showing effort), why you thought that regex would work (to prove it wasn’t just something randomly typed), and what data you’re getting with an explanation of why that result is wrong. When you show that effort, you’ll see us bend over backward to get things working for you. If you need help formatting, see the paragraph above.

    Please note that for all regex and related queries, it is best if you are explicit about what needs to match, and what shouldn’t match, and have multiple examples of both in your example dataset. Often, what shouldn’t match helps define the regular expression as much or more than what should match.


Log in to reply