Community
    • Login

    a regex question with new line

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    6 Posts 4 Posters 993 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Daniel B. 0D
      Daniel B. 0
      last edited by

      Hello,

      I need a tip on how I can read out the following values via regex from the first new line using
      the example “TEST.ONE” the time and date “20:04:59|07.05.2023” then from the third line the “TEST.ONE” 1284MB/27F and so on.

      i have tested a few things, but i always expand the regex as soon as it goes over a new line. either it always takes the whole text or it does not take the new line.

      finally output what i need is “20:04:59|07.05.2023 TEST.ONE 1284MB/27F”

      20:04:59|07.05.2023 <@OUTPUT>    TEST.ONE from floppy in backup
      20:05:19|07.05.2023 <@OUTPUT>    TEST.ONE floppy at 4.39MB/s
      20:10:54|07.05.2023 <@OUTPUT>    TEST.ONE 1284MB/27F in 5m45s at 3.72MB/s from floppy 
      20:10:54|07.05.2023 <@OUTPUT>  
      20:10:56|07.05.2023 <@OUTPUT>    TEST.TWO from floppy in backup
      20:11:13|07.05.2023 <@OUTPUT>    TEST.TWO floppy at 4.30MB/s
      20:14:17|07.05.2023 <@OUTPUT>    TEST.TWO 700MB/15F in 3m16s at 3.57MB/s from floppy 
      20:14:17|07.05.2023 <@OUTPUT>  
      20:14:18|07.05.2023 <@OUTPUT>    TEST.THREE from floppy in backup
      20:14:45|07.05.2023 <@OUTPUT>    TEST.THREE floppy at 2.32MB/s
      20:15:50|07.05.2023 <@OUTPUT>    TEST.THREE 327MB/7F in 1m26s at 3.80MB/s from floppy 
      
      PeterJonesP CoisesC 2 Replies Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @Daniel B. 0
        last edited by

        @Daniel-B-0 said in a regex question with new line:

        either it always takes the whole text or it does not take the new line.

        My guess is you had . matches newline turned on, so a greedy .* matched more than you expected. Of course, if you had shared some of the regex you tried, rather than making us reinvent the regex from scratch, it wouldn’t have to be a guess

        With . matches newline off (or using (?-s) in the regex to turn it off, ignoring the state of your box), it’s easy to control the number of lines you capture, because the number of lines you capture will depend on the number of times you have \r\n to match a windows-style CRLF line ending.

        From your problem statement, I couldn’t tell whether you were looking only for TEST.ONE, or whether you were looking for one such result for TEST.ONE, one for TEST.TWO, and one for TEST.THREE.

        Here’s something that does something similar to what I think you might want, giving you the results for each of the three groups.

        FIND = (?-s)^(\d{2}:\d{2}:\d{2}\|\d{2}\.\d{2}\.\d{4}).*\r\n.*\r\n.*<@OUTPUT>\h*(.* \d+MB/\d+F).*$
        REPLACE = $1 $2

        43f614f3-035b-474e-aab7-68a36edde4c9-image.png

        d69aa128-a5cd-487a-b5c5-77aeddb6fd1d-image.png

        ----

        Useful References

        • Please Read Before Posting
        • Template for Search/Replace Questions
        • Formatting Forum Posts
        • Notepad++ Online User Manual: Searching/Regex
        • FAQ: Where to find other regular expressions (regex) documentation
        1 Reply Last reply Reply Quote 3
        • CoisesC
          Coises @Daniel B. 0
          last edited by

          @Daniel-B-0 Guessing a bit, but I think you might want:
          ^(\S+)\h+\S+\h+(\S+)[^\r\n]*\R[^\r\n]*\R[^\r\n]*\2\h+(\S+)[^\r\n]*(\R(\S+)\h+\S+\h*)?$
          using replacement:
          \1 \2 \3

          1 Reply Last reply Reply Quote 2
          • Daniel B. 0D
            Daniel B. 0
            last edited by

            big thanks to both of you, i am happy about these two different ways that bring me to the same goal! :)

            1 Reply Last reply Reply Quote 0
            • guy038G
              guy038
              last edited by guy038

              Hello, @daniel-b-0, @peterjones, @coises and All,

              From you INPUT text :

              20:04:59|07.05.2023 <@OUTPUT>    TEST.ONE from floppy in backup
              20:05:19|07.05.2023 <@OUTPUT>    TEST.ONE floppy at 4.39MB/s
              20:10:54|07.05.2023 <@OUTPUT>    TEST.ONE 1284MB/27F in 5m45s at 3.72MB/s from floppy 
              20:10:54|07.05.2023 <@OUTPUT>  
              20:10:56|07.05.2023 <@OUTPUT>    TEST.TWO from floppy in backup
              20:11:13|07.05.2023 <@OUTPUT>    TEST.TWO floppy at 4.30MB/s
              20:14:17|07.05.2023 <@OUTPUT>    TEST.TWO 700MB/15F in 3m16s at 3.57MB/s from floppy 
              20:14:17|07.05.2023 <@OUTPUT>  
              20:14:18|07.05.2023 <@OUTPUT>    TEST.THREE from floppy in backup
              20:14:45|07.05.2023 <@OUTPUT>    TEST.THREE floppy at 2.32MB/s
              20:15:50|07.05.2023 <@OUTPUT>    TEST.THREE 327MB/7F in 1m26s at 3.80MB/s from floppy 
              

              I suppose that this regex S/R should work :

              • SEARCH (?x-is) ^ ( .+ \x20 ) <@OUTPUT> .+ \w \R .+ \R .+ ( TEST\.\u+ \x20 [\u\d]+ / [\u\d]+ ) .+

              OR

              • SEARCH (?-is)^(.+ )<@OUTPUT>.+\w\R.+\R.+(TEST\.\u+ [\u\d]+/[\u\d]+).+

              AND

              • REPLACE \1\2

              Here is your expected OUTPUT text :

              20:04:59|07.05.2023 TEST.ONE 1284MB/27F
              20:10:54|07.05.2023 <@OUTPUT>  
              20:10:56|07.05.2023 TEST.TWO 700MB/15F
              20:14:17|07.05.2023 <@OUTPUT>  
              20:14:18|07.05.2023 TEST.THREE 327MB/7F
              

              Best Regards,

              guy038

              1 Reply Last reply Reply Quote 1
              • Daniel B. 0D
                Daniel B. 0
                last edited by

                another option! thanks for this too! :)

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors