a regex question with new line
-
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
-
@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 guessWith
. 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
----
Useful References
-
@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
-
big thanks to both of you, i am happy about these two different ways that bring me to the same goal! :)
-
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
- SEARCH
-
another option! thanks for this too! :)