Advanced replace



  • Hi guys,

    I want to delete repeating text from every line but the text is slight different in each line.
    For example, every new line starts with current time using this format: HH:mm:ss
    so if for instance I have a log file and each line there was created every 1 second, it would look like this (for example):
    09:15:00
    09:15:01
    09:15:02

    How do I use the replacement function to replace them all with white space at once ?

    Thanks



  • @test-test-0 ,

    If I understand properly, you want to remove the hh:mm:ss from the beginning of the line.

    With that assumption,

    • FIND = ^\d{2}:\d{2}:\d{2}\h*
    • REPLACE = (leave empty)
    • Search Mode = regular expression

    should do what you want: it takes

    09:15:00 x
    09:15:01 y
    09:15:02 z
    

    and changes it to

    x
    y
    z
    

    You mentioned replacing with “white space”, so maybe you would want the REPLACE field to be one or more space charac ters. But your description wasn’t sufficient to be sure.

    If this doesn’t do what you want, please clarify your question. Please note the suggestions below

    ----

    Do you want regex search/replace help? Then please be patient and polite, show some effort, and be willing to learn; answer questions and requests for clarification that are made of you. All example text should be marked as plain text using the </> toolbar button or manual Markdown syntax; screenshots can be pasted from the clipbpard to your post using Ctrl+V. Show the data you have and the text you want to get from that data; include examples of things that should match and be transformed, and things that don’t match and should be left alone; show edge cases and make sure you examples are as varied as your real data. Show the regex you already tried, and why you thought it should work; tell us what’s wrong with what you do get… Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries.



  • @PeterJones said in Advanced replace:

    ^\d{2}:\d{2}:\d{2}\h*

    That’s basically what I want, but sometimes it will be slight difference in the characters order in a specific line that I want to delete.
    In simple words - sometimes the time (like 09:15:01) will be in the middle of a text line, not just in the beginning.
    Here’s an actual example of what I want to do:
    Take out those numbers next to the “Line” word, and additionally delete the date and time from each line.

    • I tried to use the syntax in FIND you suggested, unfortunately it didn’t work for me.

    30bc0180-9857-4405-beca-1015a278fbaa-image.png



  • @test-test-0 said,

    @PeterJones said in Advanced replace:

    ^\d{2}:\d{2}:\d{2}\h*

    That’s basically what I want, but sometimes it will be slight difference in the characters order in a specific line that I want to delete.
    In simple words - sometimes the time (like 09:15:01) will be in the middle of a text line, not just in the beginning.
    Here’s an actual example of what I want to do:
    Take out those numbers next to the “Line” word, and additionally delete the date and time from each line.

    When asking for search-and-replace help, context in regular expressions is everything. If you’re asking for help with regular expressions, you need to be as clear as possible. You said nothing about removing more than just the time.

    • I tried to use the syntax in FIND you suggested, unfortunately it didn’t work for me.

    Because the requirements you listed were different than the requirements you had.

    30bc0180-9857-4405-beca-1015a278fbaa-image.png

    You still haven’t followed my advice above – presenting before and after data as text (marked with the </> button so it formats as unmodified text in the forum), so that we can copy/paste your text to experiment with – so we’ve still just got your verbal description of the problem as to what you want it to look like when it is done. So there’s a good chance that this second expression I give you won’t work either, because you have not defined your problem to us sufficiently.

    I am going to assume you want to convert Line NUMBER: DATE TIME TEXT TO KEEP (where DATE and TIME match the YYYY-MM-DD HH:MM:SS,FFF format that you implied this time around; I am just assuming that NUMBER will be an integer with at least one digit) into Line: TEXT TO KEEP, but I will make the replace expression obvious enough that if you don’t want the Line: left in the final result, you can take it out.

    • FIND = (?-s)Line\x20\d+:\x20\d{4}-\d{2}-\d{2}\x20\d{2}:\d{2}:\d{2},\d{3}\x20
      • I used \x20 to indicate the space character, so that there was no question (especially at the end, where I want to delete the final space after the milliseconds as well)
    • REPLACE = Line:\x20
      • If you don’t want it prefixed with Line: and a space, just leave the REPLACE empty
    • Mode = Regular Expression

    Here is an example it works with:

    Before:

    Line 8: 2020-05-12 12:02:03,789 INFO First Line
    Line 64: 2020-05-12 12:02:06,789 INFO Second Line
    Line 512: 2020-05-12 12:02:09,789 DATA Third Line
    Line 8192: 2020-05-12 12:02:13,789 STUFF Fourth Line
    Line 16384: 2020-05-12 12:02:16,789 INFO Fifth
    Line 32768: 2020-05-12 12:02:16,789 INFO Sixth
    Line 65536: 2020-05-12 12:02:16,789 INFO Seventh Heaven
    

    After:

    Line: INFO First Line
    Line: INFO Second Line
    Line: DATA Third Line
    Line: STUFF Fourth Line
    Line: INFO Fifth
    Line: INFO Sixth
    Line: INFO Seventh Heaven
    

    If you need changes compared to this, you will have to do a better job of showing that you want to help us help you. I have already explained how to do this in my first post.


Log in to reply