So I got a text file with over 100000 lines to find and replace. Here is what I need to find:
>>DiskNum: 118
adfad
adfadf
adfadf
adfad
adafdd
>>FilePath: \\xxx\xxx\abc.tif
Replace with:
>>DiskNum: 118
adfad
adfadf
adfadf
adfad
adafdd
>>FullPath: C:\abc\abc.tif
The goal is to find >>DiskNum: 118 and the replace will happen on 6 lines after >>DiskNum: 118
Is there anyway to do it in notepadd++??
Use the following regex:
Find What: (^>>DiskNum: 118(?:\R.*){5}\R>>FilePath:\h*).*
Replace With: \1C:\\abc\\abc.tif
Regular expression: CHECKED
Details:
(^>>DiskNum: 118(?:\R.*){5}\R>>FilePath:\h*) - Group 1 (referenced with \1 from the replacement pattern) capturing a sequence of:
^ - start of a line
>>startoDiskNum: 118 - a literal char sequence
(?:\R.*){5} - 5 lines (\R is a line break, and .* matches any 0+ chars other than line break chars)
\R - line break
>>FilePath: - a literal char sequence
\h* - 0+ horizontal whitespaces
.* - the rest of the line
Related
I am trying to update specific rows starting with 0500 and update positions 31 to 35, 41 to 45 with 00000 in notepad ++ using regex. Is it something possible ? positive feedback will be really appreciated.
Code tried and got no luck
Find: ∆(0500){1}(.{5,30}).{5}(.{36,40}).{5}
Replace: \1\200000\300000
Mode: Regular Expression
You can use
Find: ^(0500.{26}).{5}(.{5}).{5}
Replace: \100000\200000
Details:
^ - start of a line
(0500.{26}) - Group 1 (\1): 0500 and any 26 chars other than line break chars
.{5} - any 5 chars other than line break chars
(.{5}) - Group 2 (\2): any 5 chars other than line break chars
.{5} - any 5 chars other than line break chars
I have this text opened in notepad++ 7.5.2 text editor :
- Note 1 message={"one":1}]
- Note 5 message={"two":2}]
- Note 2 message={"three":3}]
- Note 7 message={"four":4}]
For each line, I want to keep only the text between the brackets starting and ending brackets { } and the bracket themselves. I tried a regex - Note.* message= in https://regex101.com/ and it works. I am able to find lines in matching the regex in notepad++. But, I am not able to replace them with nothing.
How do I do the replacement ?
You may use
^- Note.* message=(.*)]$
Replace with $1. See the regex demo.
Details
^ - start of a line
- Note - - Note text
.* - any 0+ chars other than line break chars, as many as possible
message= - message= text
(.*) - Capturing group 1 ($1): any 0+ chars other than line break chars, as many as possible
] - a ] char
$ - end of a line.
Notepad++ demo and settings:
I would like to get the code that deletes every line containing the string /movie/ and the previous line (the / character is included in /movie/).
Example :
Before Code :
#EXTINF:-1,Wreck-It Ralph
http://p5.giffy.be:8080/movie/RghyHCIE4i/SDrQatrZkx/104880.mp4
#EXTINF:-1,Wrinkle-Free
http://p5.giffy.be:8080/movie/RghyHCIE4i/SDrQatrZkx/105060.mp4
#EXTINF:-1,DR | TELEMICRO 5
http://p5.giffy.be:8080/RghyHCIE4i/SDrQatrZkx/99633
#EXTINF:-1,Wrong Mistake - Short Movie
http://p5.giffy.be:8080/movie/RghyHCIE4i/SDrQatrZkx/106840.mp4
#EXTINF:-1,DR | TELESISTEMA 11
http://p5.giffy.be:8080/RghyHCIE4i/SDrQatrZkx/99632
#EXTINF:-1,Wreck-It
http://p5.giffy.be:8080/movie/RghyHCIE4i/SDrQatrZkx/104707.mp4
#EXTINF:-1,DR | TELEUNIVERSO
http://p5.giffy.be:8080/RghyHCIE4i/SDrQatrZkx/99631
After Code :
#EXTINF:-1,DR | TELEMICRO 5
http://p5.giffy.be:8080/RghyHCIE4i/SDrQatrZkx/99633
#EXTINF:-1,DR | TELESISTEMA 11
http://p5.giffy.be:8080/RghyHCIE4i/SDrQatrZkx/99632
#EXTINF:-1,DR | TELEUNIVERSO
http://p5.giffy.be:8080/RghyHCIE4i/SDrQatrZkx/99631
You can use the following regular expression:
^.*?\r\n.*?\/movie\/.*?(\r\n|$)
Step-by-step:
Open Find and Replace with Ctrl+h.
Press Alt+f to focus on Find what.
Enter the above regex.
Press Alt+g to enable regular expression mode. Ensure ". matches newline" is off.
Press Alt+a to Replace All.
How it works:
^ # anchor to beginning of line
.*? # lazily match zero or more characters
\r\n # match carriage return and line feed
.*? # lazily match zero or more characters
\/movie\/ # match literal /movie/
.*? # lazily match zero or more characters
(\r\n|$) # match carriage return and line feed or EOL
Another option is to match the first line and match a unicode newline sequence \R. Then match the second line with /movie/ and at the end match \R
Find what:
^.*\R.*/movie/.*\R
That will match
^ Start of string
.* Match 0+ times any char except newline
\R Match unicode newline sequence
.*/movie/.* Match /movie/ in the string
\R Match unicode newline sequence
Replace with:
Leave empty
Regex demo
In reference to a previous question
Python data extract from text file - script stops before expected data match
How can I capture a match and the previous two lines?
I tried this but get:
unterminated subpattern at position 0 (line 1, column 1)
output = re.findall('(.*\r\n{2}random data.',f.read(), re.DOTALL)
You may use
re.findall(r'(?:.*\r?\n){2}.*random data.*', s)
Note you can't use re.DOTALL or .* will match up to the end of the input and you will only get the last occurrence.
See the Python demo
Pattern details
(?:.*\r?\n){2} - 2 occurrences of a sequence of
.* - any 0+ chars other than line break chars, as many as possible (a line)
\r?\n - a line ending (CRLF or LF)
.*random data.* - a line containing random data substring.
See the regex demo.
I have a multiline string like this:
SA21 abcdef
BKxyz
SA21 abcdef
I need a regex that only matches if the line ^SA21 abcdef$ is present once. So it should not match for the first example but it should match for this one:
BK udsia
SA21 abcdef
BKxyz
I tried to capture the line and make sure it matches only when the same line is not found later: /(^SA21 abcdef$)(?!\1)/m regex101 but that does not work as it will probably always match the last line...
The regex you want should only match a line if the line is not present before or after the single occurrence of the line. This is achieved with a tempered greedy token:
/\A(?:(?!^SA21 abcdef$).)*(^SA21 abcdef$)(?:(?!^SA21 abcdef$).)*\z/ms
See the regex demo
The (?:(?!^SA21 abcdef$).)* is the token matching any text but the beginning of the SA21 abcdef line. The /s modifier is required so that a . could match a newline.
However, the construct is resource consuming, and it is a good idea to unroll it:
/\A(?:\n+(?!SA21 abcdef$).*)*\n*^(SA21 abcdef)$(?:\n+(?!SA21 abcdef$).*)*\z/m
See another demo
Note that \A and \z are unambiguous start/end string anchors, the /m modifier does not affect them.
Pattern explanation:
\A - start of string
(?:\n+(?!SA21 abcdef$).*)* - zero or more sequences of:
\n+ - 1 or more newlines ...
(?!SA21 abcdef$) - not followed with SA21 abcdef that is the whole line
.* - zero or more chars other than a newline
\n* - zero or more newlines
^ - start of a line
(SA21 abcdef) - the line that must be single
$ - end of line
(?:\n+(?!SA21 abcdef$).*)* - see above
\z - end of string.