deplace a block to an other place



  • hello
    i have a lot of html files with inside
    <noscript class"text" lang=“example”>
    line1
    line2
    line3
    line4
    </noscript>
    in each html file i have a noscript tag with 4 or 5 lines inside, but for each file the lines are different
    i would copy all this noscript block to an other place in the same html file for all my html file

    is there a ways for notpad++ understand to copy all wht it find inside the noscript tag of a file in an oter place of the same file?

    what solution?
    thanks a lot



  • What you want is a range search-replace operation. I haven’t found an easy way to do it in Notepad++. There are specialized apps out there. You might want to google “ecobyte replace text”. Be careful though. Good luck!



  • With textfile =

    <noscript class"text" lang=“example”>
    line1
    line2
    line3
    line4
    </noscript>
    
    <noscript class"text" lang=“example”>
    lineA
    lineB
    lineC
    lineD
    </noscript>
    

    I used Search: <noscript.*?>\s*\K.*?(?=</noscript>) with “. matches newline”, and it highlighted just line1-line4 or lineA-lineD

    • <noscript.*?>\s* = finds the <noscript blah> opening, and any space/newline after it (.*? = see below)
    • \K = don’t include anything before the \K in the highlighted portion
    • .*? = match 0 or more characters, but as few as possible. This makes it non-greedy, so it doesn’t try to swallow the first </noscript>
    • (?=</noscript>) = a positive-lookahead: the </noscript> must follow what’s already been matched, but it won’t be included in the highlighted section

    So the steps I do:

    1. ^F (Find): <noscript.*?>\s*\K.*?(?=</noscript>)

    2. Find Next, it highlights

      line1
      line2
      line3
      line4
      
    3. I can manually copy/paste it wherever I want

    4. Find Next will find the next instance, in my example,

      lineA
      lineB
      lineC
      lineD
      
    5. and I can choose to manually copy/paste the lineA-lineD version anywhere I want.

    If you have some indicator that shows where the final PASTE should happen, you could even turn it into a fancier search/replace to get it automated…



  • Linguistic issue

    The OP kat75 let us know he had difficulties in English. deplace is not in the English dictionary. I believe he means replace as in range search-replace.

    kat75 might want to follow my advice above and google ecobyte replace text. Hopefully, kat75 has a friend who is fluent in both English and their native language. kat75 should experiment with range search-replace in a temp directory first.

    Copy a few html files to C:\Temp and test there until the results are as desired. That’s how I did and everything worked flawlessly.





  • I know there’s no such word as “deplace”. Based on the text in the post, “i would copy all this noscript block to another place in the same html file”, I understood that @kat75 wanted to copy text from one place in a document (leaving it there), and also paste it to a second place in the same document. My post described the steps necessary to semi-manually do it, with automatically selecting the source text.

    For example, I am assuming @kat75 wanted to take a file like

    <noscript class"text" lang=“example”>
    line1
    line2
    line3
    line4
    </noscript>
    
    ... lots of document ...
    
    <pre>
    -=-=-= REPLACE THIS SPECIFIC LINE WITH WHATEVER WAS IN THE EXAMPLE NOSCRIPT =-=-=-
    </pre>
    

    and replace it with

    <noscript class"text" lang=“example”>
    line1
    line2
    line3
    line4
    </noscript>
    
    ... lots of document ...
    
    <pre>
    line1
    line2
    line3
    line4
    </pre>
    

    This could be done manually as I described before (using the previous FIND string, manually copying, manually moving the cursor, and manually pasting).

    Or, as I added, it could be done with a Search and Replace, which I didn’t supply at the time. I was hoping @kat75 would chime in with more details, so I wouldn’t spend more time on a solution to my interpretation of the problem, without further input…

    But since you seem to think NPP is not capable of this task, when it is: Using my new example file above, my interpretation of the resultant file desired would be

    <noscript class"text" lang=“example”>
    line1
    line2
    line3
    line4
    </noscript>
    
    ... lots of document ...
    
    <pre>
    line1
    line2
    line3
    line4
    </pre>
    

    This can be accomplished in Notepad++ using a search-and-replace:

    • Find What: (<noscript.*?>\s*)(.*?)(\s*</noscript>.*)(^-=-=-= REPLACE THIS SPECIFIC LINE WITH WHATEVER WAS IN THE EXAMPLE NOSCRIPT =-=-=-$)
    • Replace with: $1$2$3$2
    • ☑ Regular Expression and ☑ . matches newline
    • REPLACE

    If, instead, @kat75 wanted the entire noscript, including the opening and closing tags, a simple modification will do the trick:

    • Find What: (<noscript.*?>.*?</noscript>)(.*)(^-=-=-= REPLACE THIS SPECIFIC LINE WITH WHATEVER WAS IN THE EXAMPLE NOSCRIPT =-=-=-$)
    • Replace with: $1$2$1
    • ☑ Regular Expression and ☑ . matches newline
    • REPLACE

    which would result in

    <noscript class"text" lang=“example”>
    line1
    line2
    line3
    line4
    </noscript>
    
    ... lots of document ...
    
    <pre>
    <noscript class"text" lang=“example”>
    line1
    line2
    line3
    line4
    </noscript>
    </pre>
    

    If @kat75 wants the noscript contents or the full pair to be able to go after the indicated line rather than replacing it:

    <noscript class"text" lang=“example”>
    line1
    line2
    line3
    line4
    </noscript>
    
    ... lots of document ...
    
    <pre>
    -=-=-= INSERT TEXT AFTER THIS LINE =-=-=-
    </pre>
    

    Then to get the contents without the tags, use:

    • Find What = (<noscript.*?>\s*)(.*?)(\s*</noscript>.*)(^-=-=-= INSERT TEXT AFTER THIS LINE =-=-=-$)
    • Replace With = $1$2$3$4\r\n$2

    or to include the noscript tags as well:

    • Find What = (<noscript.*?>.*?</noscript>)(.*)(^-=-=-= INSERT TEXT AFTER THIS LINE =-=-=-$)
    • Replace With = $1$2$3\r\n$1

    If none of these accomplish what @kat75 wants, then @kat75 needs to chime in with example “before” and “after” text, so that we don’t have to guess.

    (And I’m sure @guy038 would be able to improve on my regex…)
    [edit: added paragraph break]



  • @PeterJones

    How did you make the little checkboxes in text? That’s pretty cool. I’ve read about some of the markdown used on this forum, but not all of it is clear to me. Not that I’m saying the checked-checkboxes are markdown…

    I’m talking about this:
    ☑ Regular Expression

    How would one do an empty checkbox as well as a checked one?

    Thanks!



  • Copy/paste. :-) I have an always-open scratchpad in NPP that includes some often-used characters:

    Checkboxes: `☐ ☑ ☒ ✓ ✗`
    

    Windows’ charmap.exe > Character Set: Unicode > Group By: Unicode Subrange > Symbols & Dingbats has the checkboxes in the first row when I’m using Font: DejaVu Sans Mono.

    I also like using http://www.fileformat.info/info/unicode/char/search.htm to find Unicode characters by name or idea (like “arrow” or “box” or “check”). (I’ve actually got it as one of my custom search engines in Firefox, with the keyword “unicode”, so I just have to type “unicode ballot box” in my address bar to get http://www.fileformat.info/info/unicode/char/search.htm?q=ballot+box&preview=entity.



  • hello all
    a lot of thanks for your answer
    i am french and not perfectly fluent in english
    thanks peterjones
    i have tried
    Find What = (<noscript.?>.?</noscript>)(.*)(^-=-=-= INSERT TEXT AFTER THIS LINE =-=-=-$)
    Replace With = $1$2$3\r\n$1

    my files are like this

    1.html
    <noscript class"text" lang=“example”>
    line1
    line2
    line3
    line4
    </noscript>
    some text…
    etc…
    etc
    <img… and …style=“display:none;”></div>

    2.html
    <noscript class"text" lang=“otherexample”>
    otherline1
    otherline2
    otherline3
    otherline4
    </noscript>
    othersome text…
    etc…
    etc etc
    <img… other and …style=“display:none;”></div>

    i would to move (to move = déplacer in french) in each file all this <noscript… tag with the tag (generally inside there are 4 lines… can be 5)) i would to move it in each page just after style=“display:none;”></div>
    and i would to do this for more than 400 files in knowing that all my noscript are differents in each page
    warning; in each file i have 2 noscript tag one is just <noscript> the other is like <noscript class"text" lang=“example”>
    its this second that i would to move

    not easy :-)



  • What you said you tried for the search/replace didn’t quite match my examples, but that’s okay. I will customize it based on what you’ve given me:

    • Find = (<noscript[^>]*?class=*["“][^"“”]*?["”][^>]*?lang=["“][^"“”]*?["”]>.*?</noscript>\R*)(.*?)(<img[^>]*?style=["“]display:none;["”]></div>)
    • Replace = $2$3\r\n$1
    • Assumptions/Changes
      • I used [>]*? to include various unknown attributes, assuming they don’t contain a >: if they do, this won’t work
      • I used expressions that were ambivalent about smart quotes “” and normal quotes "", because you switched back and forth. Personally, I doubt there are actual smart quotes for the attributes in your HTML, but this allows my expression to work on exactly the text that you put in your examples, or on text that I think is well-formed HTML
      • Similary, I used class=* to allow class="text" or class"text", because I believe class"text" is invalid HTML, but I wanted the expression to work on your example text, just in case you are processing invalid HTML.
      • I allowed anything (or, rather, any non-quote character) inside the class="text" and lang="example", so it’s not restricted to exactly text or example: I saw you had example and otherexample, so I wanted to make sure that text and othertext would both work, too.
      • Since there is a language barrier, I am assuming by “to move = déplacer” you mean "move it, completely deleting the block from the beginning, and instead putting it after the <img display:none;">...</div>"

    So that should work manually on the two example files you gave me: when I copy/pasted from your post into two files, then ran the exact FIND and REPLACE listed above once per file, it successfully found the noscript and moved it after the img.

    HOWEVER (and this is a big “however”), that won’t be fun for hundreds of files. I tried having NPP open with just the two example files, then I used the Replace All in All Opened Documents option, and it replaced it in both files. Then I copied the originals so I had 100 files, and I opened them ALL in Notepad++, and the “Replace All in ALl Opened Documents” still did what I wanted. So it might work if you just open all 400 hundred files, though there’s no guarantee: I might do it in smaller groups of 50-100 files, depending on how large your files are.

    If you are going to be frequently making changes like this, but the rules are changing, I highly recommend learning a scripting language: I would have done this from the command line with Perl; many others here would have used Python or Lua. The benefit of the latter two is that there are plugins for automating Notepad++ using the Python Script Plugin or the LuaScript Plugin. @Claudia-Frank could proably write the Python Script to open each of the 400 files, apply my regex, and save and close them without much difficulty. Unfortunately, I am not a Python expert, so it would take me hours more than I have available to muddle through it. (It may even be that @go2to’s ecobyte-link provides such a scripting language, or a simple interface for applying a particular regex to a multitude of files.)



  • a great thanks peterjones
    perhaps the solution for me will be to joint each file x2 then remove the lines i dont want

    in dos do you know how to join in one command each file of a directoty with itself
    for one file its easy
    copy name.htm +name.htm d:\temp\name.htm

    time to sleep for me i shall search for this tomorrow
    *do you know this french free texteditor http://www.gbesoft.fr/gbepad.php
    its not bad, i use it in association wth notepad++



  • A script could look like this, so if you are interested in
    a python script solution let me know and we can work it out.

    # -*- coding: utf-8 -*-
    
    import os
    FIND_WHAT = u'FIND_WHAT_REGULAR_EXPRESSION'
    REPLACE_WITH = u'REPLACE_WITH_EXPRESSION'
    
    DIRECTORY_OF_FILES = u'THE_DIRECTORY'   # like c:\\temp\\directory - doublebackslash needed
    
    os.chdir(DIRECTORY_OF_FILES)
    list_of_files = [x for x in os.listdir('.') if x.endswith('.html')]
    for file in list_of_files:
        notepad.open(file)
        editor.rereplace(FIND_WHAT, REPLACE_WITH)
        notepad.save()
        notepad.close()
    

    This script looks for html files in a defined directory.
    If your files are under certain different subdirectories we need to modify the code.

    Cheers
    Claudia



  • Hi, @kat75,

    Here is my contribution, to your problem ;-)

    Let’s suppose you have two files 1.html and 2.html, as below :

    <!-- 1.html -->
    Some text
    before the
    "noscript" tag
    <noscript class"text" lang="example">
    line1
    line2
    line3
    line4
    </noscript>
    bla
    bla
    bla
    blah
    <img… and …style="display:none;"></div>
    And some text,
    located after
    ............
    <noscript>
    ............
    

    and

    <!-- 2.html -->
    bla
    bla
    <noscript>
    bla
    
    <noscript class"text" lang="otherexample">
    Other line1
    Other line2
    Other line3
    Other line4
    Other line5
    </noscript>
    Other text…
    etc…
    etc…
    etc…
    <img… other and …style="display:none;"></div>
    Other Text
    ..........
    till the END of the file
    
    • Move back, at the very beginning of current file ( Ctrl + Origin )

    • Open the Replace dialog ( Ctrl + H )

    • Select the Regular expression search mode

    • In the Search what: box, type in the regex (?s)(<noscript class.+?</noscript>\R)(.+?style="display:none;"></div>\R)

    • In the Replace with: box, type in the regex \2\1

    • Click on the Replace All button

    => Your two html files should be modified, as below :

    <!-- 1.html -->
    Some text
    before the
    "noscript" tag
    bla
    bla
    bla
    blah
    <img… and …style="display:none;"></div>
    <noscript class"text" lang="example">
    line1
    line2
    line3
    line4
    </noscript>
    And some text,
    located after
    ............
    <noscript>
    ............
    

    and

    <!-- 2.html -->
    bla
    bla
    <noscript>
    bla
    
    Other text…
    etc…
    etc…
    etc…
    <img… other and …style="display:none;"></div>
    <noscript class"text" lang="otherexample">
    Other line1
    Other line2
    Other line3
    Other line4
    Other line5
    </noscript>
    Other Text
    ..........
    till the END of the file
    

    I do hope that it’s the kind of Search/Replacement what you expect to !


    Notes :

    • I suppose that the ending tag </noscript> ends the current line, in your html files

    • I suppose the the text <img………style="display:none;"></div> ends the current line, too

    • As usual, the (?s) modifier means that a dot regex character represents any single character ( Standard or EOL chars )

    • Then, the part <noscript class.+?</noscript>\R looks for any complete individual noscript block ( Note that the form .+? stands for the smallest non-null range of characters, between the two strings <noscript class and </noscript> )

    • This noscript block, with the final EOL characters ( \R ), is stored as group 1, due to the enclosed parentheses

    • Finally the part .+?style="display:none;"></div>\R searches for the smallest range of characters till the string style=“display:none;”></div>

    • Again, this block of lines, with the final EOL characters ( \R ), is stored as group 2, due to the enclosed parentheses

    • In replacement, we just rewrite these two groups, in a reverse order ! ( \2\1 )


    Remarks :

    • As previously said, for 400 files about, you should use the Replace in Files feature ( Ctrl + Shift + F )

    • Group all your html files, which have to be modified, in a particular folder

    • Do a BACKUP of this folder ( One never knows ! )

    • Fill up the Filters and Directory boxes, too

    • Click on the Replace in Files button, to perform a global replacement, on all your files

    • IMPORTANT : Do NOT use the v7.4 or v7.4.1 version of N++. Indeed, with these last versions, there a bug, when multiple S/R are performed : a part of the files scanned are not changed -:((( Refer to :

    https://notepad-plus-plus.org/community/topic/13865/replace-all-in-all-open-documents-problem-in-7-4-1/1

    Cheers,

    guy038



  • hello
    just awake
    thanks peter, claudia and guy
    guy its magic, seems to work fine, i didnt thank that it was so easily possible
    i must know how it works and donwload a previous version of notepad++ i have v7.41
    really fantastic
    a great thanks



  • @kat75 Guy often performs magic with Regular Expressions. :) And I believe he speaks French also.

    A good starting point for Regular Expressions is http://www.regular-expressions.info/



  • thanks glen for the link
    i shall try to learn regular expressions, there are really possibilities



  • hello
    i have a question
    i am trying to understand how what has given to me guy works
    (?s)(<noscript class.+?</noscript>\R)(.+?style=“display:none;”></div>\R)

    if i try to get an other sequence between a a tag by example… i got nothing! what is wrong?

    (?s)(<a href.+?</a>\R)

    with an img tag it doesnt take the first “> why? :-) thanks
    (?s)(<img class.+?”>\R)



  • @kat75

    without having the data we cannot say for sure what went wrong.
    So, in theory, the hyperlink regex would match everyhting within the tag if
    closing tags is directly followed by an eol char (no space)
    and the image regex would match starting with <img class followed by
    anything until ”> appears, followed directly by an eol char.

    Cheers
    Claudia



  • at the end it must be a eol character, ok i understand now



  • i would to say thanks to all, i have well succeed to move my block and begin to understand a bit notepad++
    is there a way to send private messages on notepad community, i dont find how


Log in to reply