Regex: Copy text between one html file to more html files (from a folder to another)
-
In folder 1 I have a file1.html with this code
<!-- PART1 --> <div class="Field"> <div align="right"> TEXT TEXT A A A </div> </div> <!-- PART2 -->
In folder 2 I have more html files with this kind of code:
<!-- PART1 --> <p>Message from my computer:</p> <p><samp>File not found.<br>Press F1 to continue</samp></p> <!-- PART2 -->
So the content between
<!-- PART1 -->
to<!-- PART2 -->
in file1.html needs be copied in all html files in folder 2 at the same<!-- PART1 -->
to<!-- PART2 -->
place.Is it possible to make this parsing just with a REGEX formula?
I know that is possible with PowerShell, but I would prefer something simpler and faster with regex only. Is this possible?
# Sourcefile contains text to insert $sourcefile = "c:/Folder1/file1.html" # Get content to insert $sourceContent = Get-Content -Path $sourcefile -Raw # Get target files $destinationFiles = Get-ChildItem -Path "c:/Folder2" -Filter "*.html"; # Do for each file in destination folder foreach ($file in $destinationFiles) { # Prepare regex $contentToInsert = [regex]::match($sourceContent,'(?ms)<!-- PART1 -->(.+)<!-- PART2 -->').value # Get content of destination file $destinationContent = Get-Content $file.FullName -Raw # Replace the text in destination file content $destinationContent = $destinationContent -replace '(?ms)<!-- PART1 -->(.+)<!-- PART2 -->',$contentToInsert # Write back destination file Set-Content -Path $file.FullName -Value $destinationContent -Encoding UTF8 } #end foreach file
-
Is it possible to make this parsing just with a REGEX formula?
Maybe we need wording in the N++ user manual for what regular expression replacement cannot do.
File to file, folder to folder, these are good candidates.
I mean, well, it should be obvious because there aren’t any statements in the relevant sections about these types of things, but we get questions like these.
At least if it were in the manual, it would be possible to brush off such questions with a RTFM. -
@Alan-Kilborn said in Regex: Copy text between one html file to more html files (from a folder to another):
Is it possible to make this parsing just with a REGEX formula?
Maybe we need wording in the N++ user manual for what regular expression replacement cannot do.
File to file, folder to folder, these are good candidates.
I mean, well, it should be obvious because there aren’t any statements in the relevant sections about these types of things, but we get questions like these.
At least if it were in the manual, it would be possible to brush off such questions with a RTFM.I don’t think a user manual should have to maintain a list of things the application cannot do. That seems a better job for the FAQ section of the forum, instead.
-
Hi, @robin-cruise, @alan_kilborn, @peterjones and all,
So, Robin, if I fully understand you, you would like to replace any zone
<!-- PART1 -->•••••••••<!-- PART2 -->
in allHTML
files of the sub-folderfolder 2
with the zone, below :<!-- PART1 --> <div class="Field"> <div align="right"> TEXT TEXT A A A </div> </div> <!-- PART2 -->
If I’m right about it, that’s rather easy with regexes :
-
Backup all your
HTML
files offolder 2
within an other folder ! ( A common sense action ! ) -
Open the Find in Files dialog (
Ctrl + Shift + F
)-
SEARCH
(?s-i)<!-- PART1 -->.+?<!-- PART2 -->
-
REPLACE
<!-- PART1 -->\r\n <div class="Field">\r\n <div align="right">\r\n TEXT TEXT A A A\r\n </div>\r\n </div>\r\n<!-- PART2 -->
-
FILTERS
*.html
-
DIRECTORY
The ABSOLUTE location of "folder 2"
-
Select the
Regular expression
search mode -
Click on the
Replace in Files
dialog -
Hit the
OK
button of the Are you sure? dialog
-
So, @alan-kilborn and @peterjones, in this specific case, the goal to reach seems still possible with regexes ;-))
Best Regards,
guy038
-
-
@guy038 said in Regex: Copy text between one html file to more html files (from a folder to another):
in this specific case, the goal to reach seems still possible with regexes ;-))
Actually I think you missed the statement that file1.html (in 1 folder) contains the source “code” that needs replacing a “no code present” area contained within many files in a different folder. Hence as the others mentioned, working with 2 files simultaneously, which regex is not capable of doing.
I had considered mentioning that appending file1.html to ALL the other files, then the “Find in Files” option (with a suitable regex) would be able to:
- move the code to the new location and
- remove the remainder of the file1.html string that was appended.
But then I thought this is far more work than his already thought about (and supplied) powershell solution.
Terry
-
Hi, @robin-cruise, @terry-r, @alan-kilborn, @peterjones and all,
@terry-r :
Many thanks for your comments However, I’m sorry as I still don’t see in which way I’m wrong :-((
@robin-cruise said :
So the content between <!-- PART1 --> to <!-- PART2 --> in file1.html needs be copied in all html files in folder 2 at the same <!-- PART1 --> to <!-- PART2 --> place.
In my regex S/R, the part of text to be copied from
File1.html
is placed in the replace zone :- REPLACE
<!-- PART1 -->\r\n <div class="Field">\r\n <div align="right">\r\n TEXT TEXT A A A\r\n </div>\r\n </div>\r\n<!-- PART2 -->
and will replace, as @robin-cruise said, the same
<!-- PART1 -->•••••••<!-- PART2 -->
area in all files offolder 2
with the search zone :- SEARCH
(?s-i)<!-- PART1 -->.+?<!-- PART2 -->
So, I’m missing something obvious ! Could someone enlightens me with a short
file1.html
text, supposed to be infolder 1
and a shortfile2.html
text, supposed to be infolder 2
? TIA !BR
guy038
- REPLACE
-
@guy038 said in Regex: Copy text between one html file to more html files (from a folder to another):
So, I’m missing something obvious !
Your solution involves grabbing the “source” first, then using that in the 2nd step which is the replacement using Find in Files. The OP was asking whether regex can simultaneously grab the source data, then edit the “other” files and insert it into all those files. So his request was to “open” 2 files together and “regex” them.
Of course your solution would work if the “source code” is short enough.
Terry
-
Hi, @robin-cruise, @terry-r, @alan-kilborn, @peterjones and all,
Ah…yes, I see what you mean, Terry !
Now, let’s imagine that Notepad++ has these two new native features ( just a dream !! ) :
-
A
Mark in Files
dialog, in the same way than theFind in Files
dialog. So, we could just bookmark any<!-- PART1 -->•••••••<!-- PART2 -->
area, in allHTML
files offolder 2
, with, of course, the optionPurge for each search
ticked -
A
Bookmark in Files
dialog, too. So, we could :-
Select and copy the specific
<!-- PART1 -->•••••••<!-- PART2 -->
section ofFile1.html
in the clipboard withCtrl + C
-
Then, run the
Search > Mark > Paste to (Replace) bookmarked lines
option, in allHTML
files offolder 2
-
Best Regards,
guy038
-
-
This post is deleted! -
thanks @guy038 Beautiful !
-
Hello, @robin-cruise, @terry-r, @alan-kilborn, @peterjones and all,
Well, I just realize this morning, that I’m wrong again, in my previous post. :-((
Indeed, as several consecutive lines would be marked, the
Search > Mark > Paste to (Replace) bookmarked lines
option would replace any bookmarked line with clipboard contents ! whereas the paste action should occur, once only, replacing all the lines bookmarked !As a conclusion, this kind of operation, between files, cannot be achieved with regexes, as @peterjones and @alan-kilborn stated, previously !
BR
guy038
-
@guy038 said in Regex: Copy text between one html file to more html files (from a folder to another):
has these two new native features ( just a dream !! )
Keep in mind that these “dream” features would have to be “Mark in All Opened Documents” and “Bookmark in All Opened Documents”…and thus the files in question would all have to be open in Notepad++.
The reason is that bookmarks and marks from marking are not saved with a file, so if a file was not open, there would be no remembering.
I know it was just a fantasy, but sometimes we have to do a little grounding of fantasy. :-)
-
Hi, @alan-kilborn and All,
I really should have thought a bit more, before answering ! Indeed, Alan, although the bookmarks are kept in the
session.xml
configuration file, there are not stored in files themselves. So, as soon as a file containing bookmarks is closed, these bookmarks are gone away the next time the file is opened, during a new N++ session !Sorry for all that noise !
Cheers,
guy038