Replacing text at line-end



  • I have something like this.
    dsl.JPG
    I want:

    • to remove all the [/m]'s from the first line of each group (i.e. a[/m] and a dua[/m]);
    • change all the [/m]'s at the end of each line beginning with [m0] to [/m0);
    • change all the [/m]'s at the end of each line beginning with [m1] to [/m1], etc.
      How can I do it? Please help. I’m using Notepad++. Thanks!


  • @Polar-Bear said in Replacing text at line-end:

    I have something like this.

    Hi, welcome to the Notepad++ forum.
    Your problem can be completed with a number of different replace processes, however with a bit more thought I’ve been able to combine it all together in 1 regular expression.

    So, using the “Replace function” we have
    Find What:(?-s)^(([^ ].*)(\[/m\]))|((\[m([01])\].+)(\[/m\]))$
    Replace With:(?1\2)(?4\5[/m\6])

    As this is a regular expression the search mode MUST be “regular expression” and please have “wrap around” ticked. You can copy and paste the red text above into their respective fields in the Replace function and then hit the “Replace All” button. As always it might pay to have a backup copy of the file before completing the replacement, just in case something isn’t right. You can also use just the “Find Next” button and then the Replace button to do a line at a time, at least initially to see what is changing.

    The reason I say to check is that by providing a image of your text it means I’ve had to recreate that myself. So for the indented lines I can’t be sure of whether they are “tab” indented or just a single space character, I’ve gone with the “space”. It’s preferable to supply the example in text form (so we can copy it for testing) and to do so you’d use the backtick (3 times) followed by the letter z on 1 line, then the example lines below that, followed by the 3 backticks again on another line following. This information is provided in the FAQ section in the post titled “Request for help without sufficient information…”.

    Good luck
    Terry



  • @Terry-R It does work. Thanks a lot!
    However, could you do me one more favor?
    In my file, I still have [m3], [m4], etc., and the syntax doesn’t apply for those, i.e. the [/m]'s at the end of those lines still keep. So, what should I do to for the [/m] at the end of the remaining lines to change to [/m3], [/m4].
    I’m a novice and don’t have any ideas of the combination you suggested.
    Thanks.



  • @Polar-Bear said in Replacing text at line-end:

    one more favor?

    Actually that is easy. Where you see the 01 inside square brackets, expand that so it becomes 0123456789 or any combination of numbers that you are looking for. Given how I create the /m at the end of the line, it will automatically correspond to the first number at the start.

    Terry



  • Hello @polar-bear, @Terry-r and All,

    Here is a second solution which :

    • Does not care about contents in a [....] block

    • Does not care about possible leading blank characters ( Tab or Space chars ) before each [....] block

    • Does not care about missing / character in the [....] block at end of line

    So, assuming this text :

    a dua[/test]
    [k0]verb[m]
     [m1]❖ To ape, to chime in, to join in, to take a leaf out of sb's book[/bla bla]
    

    The following regex S/R :

    SEARCH (?-s)(^\h*\[(.+?)\].+|)\[.+?\]$

    REPLACE ?2\1[/\2]

    would give :

    a dua
    [k0]verb[/k0]
     [m1]❖ To ape, to chime in, to join in, to take a leaf out of sb's book[/m1]
    

    Notes :

    • As usual the (?-s) in-line modifier ensures that the regex dot symbol represents a single standard character and not a line-break character

    • Then, the search regex contains an alternation symbol |. So it corresponds to, either :

      • The regex A (^\h*\[(.+?)\].+)\[.+?\]$ :

        • The first part ^\h*\[(.+?)\].+, before the ending [....] block, \[.+?\]$, is stored as Group 1

        • After possible leading blank chars, at beginning of line, ^\h*, looks for a [....] block, whose contents are stored as Group 2, \[(.+?)\], following with a non-null range of standard chars, .+ till a last [....] block at end of line, \[.+?\]$

      • The regex B \[.+?\]$ :

        • Searches for a [....] block, whatever its contents, \[.+?\], ending the line $
    • In the replacement regex ?2\1[/\2] :

      • The (?#....) or ?#.... syntaxes represents a conditional replacement, related to Group # . So :

        • If Group 2 exists ( case of Regex A ) we rewrite the Group 1 first, followed with the literal string [/, then, with Group 2 ( contents of the leading [...] block ) and, finally, with the literal ] char

        • If Group 2 does not exist ( case of Regex B ), we rewrite nothing, so it deletes the ending [....] block

    Best Regards,

    guy038



  • @guy038 I don’t know how to express my thanks for your quick and effective answer. I spent a whole day looking for the answer on the Internet, but in vain.
    As for your explanation, I have tried to figure out, but understand very little. But when I used the syntax you gave and carried out the task (with some sense of uncertainty) - it worked like magic. Now, I can formatting each line (text color, bold type, etc.) as I wish.
    Thanks again.
    Have a good day!
    Polar Bear



  • Hello, @polar-bear, @terry-r and All,

    To @polar-bear :

    For a quick overview of regular expressions, begin here :

    https://www.regular-expressions.info/quickstart.html

    You’ll find additional information here


    Now, after posting my previous post, I realized, with boredom, that most of my regexes were erroneous :-((

    After a while, I understood that everywhere I wanted to write the strings \[ and \] the N++ NodeBB forum just writes [ and ] !!

    So, for instance, in order that my previous search regex was written, in the red code form, exactly, like below :

    SEARCH (?-s)(^\h*\[(.+?)\].+|)\[.+?\]$
    

    It must be written, with double escape sequences, while replying :

    **`(?-s)(^\h*\\[(.+?)\\].+|)\\[.+?\\]$`**
    

    To @terry-r :

    As soon as I corrected all my regexes of my previous post, I immediately checked if your search regex was correctly expressed, too ! And , indeed, your regex syntax is totally correct ! So, you already know this mandatory syntax when you want to write the strings \[ and \[ ;-)) I was not aware of that specificity !

    Luckily, after testing all the other special regex chars, preceded with backslash, the square brackets are the only chars that need to be preceded with a double backslash \\


    Now the downside is I’ve written 40 posts about, containing this wrong syntax of my regexes, which need corrections ! Well, one more task to be done :-(( Surprisingly, I’m wondering how each OP tried to make the regex work ? Probably, some of them add the \ before the [ and ] characters, by themselves ! Nice, they’re improving ;-))

    Best Regards,

    guy038



  • @guy038 said in Replacing text at line-end:

    After a while, I understood that everywhere I wanted to write the strings [ and ] the N++ NodeBB forum just writes [ and ] !!

    @PeterJones and I had this same discussion…last week maybe?

    We thought however that it is only if you start an expression with backslash-bracket. Maybe it goes deeper than that?

    I’d like to find our postings, but I can’t.
    The web site software used to allow you to click on a user and review their posting history, going back as far as you’d like.
    This newer software puts a very short limit on it, sadly (unless someone knows a workaround?).
    I also tried searching postings for [ but that didn’t work so well.

    Apparently even double-backslashing doesn’t do something in front of a [ or ] in certain circumstances, see:

    fadfac90-376d-4820-8087-e176e2f6d54c-image.png and it still renders without any backslash!



  • @guy038 said in Replacing text at line-end:

    I immediately checked if your search regex was correctly expressed, too

    Yes, that was ALMOST a gotcha for me as well. I was not aware of this issue, however I did proof my post once posted. Of course that then gave me little time to spot ALL the issues in my regex. Luckily I did that just in time.

    Do we perhaps have to express these regexes inside of some other “markdown” code to prevent further issues. I must admit to not being very worldy in terms of all the codes markdown can “tolerate”.

    Terry



  • @Terry-R

    backslash-bracket is an uncommon usage, but I wouldn’t call it rare, so yes, it does call for some other type of markdown.

    Code?:

    \[
    

    I guess that works, if you remember to double the backslash there.

    I’m unsatisfied. :-(



  • @Alan-Kilborn said in Replacing text at line-end:

    @PeterJones and I had this same discussion…last week maybe?

    We thought however that it is only if you start an expression with backslash-bracket. Maybe it goes deeper than that?

    I’d like to find our postings, but I can’t.

    https://community.notepad-plus-plus.org/post/55595

    The web site software used to allow you to click on a user and review their posting history, going back as far as you’d like.
    This newer software puts a very short limit on it, sadly (unless someone knows a workaround?).

    You should be able to go through your own posts readily: go to your own profile, click the vertical ellipsis , and select Posts. Yeah, I just went to your profile, ⋮ > Posts, and was able to live-scroll, and it kept extending. So the main profile page only shows recent and best, but you can delve deeper using . (edit: url for your livescroll appears to be https://community.notepad-plus-plus.org/user/alan-kilborn/posts , so /user/<username>/posts should get you to the person’s posts readily /edit)

    Alternately, Search > Advanced, Posted By = username, Sort By = Post Time.

    I also tried searching postings for [ but that didn’t work so well.

    Searching for code operators (or code containing operators) using your favorite search engine has similar difficulties, so that one’s not this forum’s fault. :-)



  • @PeterJones

    https://community.notepad-plus-plus.org/post/55595

    @guy038 should have remembered that thread; it wasn’t that long ago and he upvoted it, unless he very recently upvoted it. :-)

    You should be able to go through your own posts readily…click the vertical ellipsis…

    YES! Able to do mine or anyone else’s that way. THANKS!

    so that one’s not this forum’s fault

    Right, of course I wasn’t blaming the forum software for not being able to search for [. But I wonder what the minimum is for a search; for example I tried searching for the and got zero hits, so it must be ignoring such a search…



  • Hi, @polar-bear, @terry-r, @alan-kilborn, @peterjones and All,

    Alan, no problem about reaching all my posts because I keep all of them in a specific folder and each of them begins with its address, either on our old forum, on SourceForge.net or on our present NodeBB forum ;-))

    So, after getting rid of all posts related to SourceForge.net ( Read only forum, by now, anyway ! ), I have 32 posts to verify !


    BTW, Terry, there is a post that concerns you ! Refer to :

    https://notepad-plus-plus.org/community/topic/15922/this-might-need-a-lookbehind-which-is-beyond-me-can-someone-provide-the-solution/8

    Of course, I have not modified this post, yet ! But, You’ll easily see why the given regexes, as well as in my subsequent posts, are erroneous :-((

    Best Regards,

    guy038

    P.S.

    Now, Alan, I remember your discussion between you and Peter but I must admit that I didn’t pay much attention to it ! Sorry :)



  • @guy038 said in Replacing text at line-end:

    BTW, Terry, there is a post that concerns you ! Refer to :

    Crikey, that’s about where it all began for me! The reason I got involved in the forum. I see now that the \ is missing in the crucial spots, but that raises an interesting question. I just went back and checked what I did actually use, and I see I inserted the \ where required, so I (or someone) put me on the right path back then.

    So as a test, can I insert this code with red highlighting inside of the black box?

     ```z
     Find What:`(?-s)^(([^ ].*)([/m]))|(([m([01])].+)([/m]))$`
     Replace With:`(?1\2)(?4\5[/m\6])`
     ```
    

    The right window doesn’t show it as such, will have to wait and see once posted.
    No no red code. Is red highlighting possible inside the black box?

    Cheers
    Terry



  • Hello, @terry-r and All,

    After numerous tests, here are my conclusions about the insertion of ASCI symbols, with Unicode code-point < \x{007F}, in replies on NodeBB forum

    Four cases will be considered :

    • Inside Normal text, possibly emphasized :

      • Writing of the symbol itself

      • Writing of the symbol, preceded by a backslash \

    • Inside Code Span zones or Code Block zones ( delimited between two lines of 3 backticks or two lines of 3 underscores ) or text indented with 4 space chars :

      • Writing of the symbol itself

      • Writing of the symbol, preceded by a backslash \

    - If inside NORMAL text, possibly EMPHASIZED :
    
    •------•--------*           •---------•--------*  
    | Type | To get |           |  Type   | To get |
    •------•--------*           •---------•--------*
    |  \[  |   [    |           |  \\\\[  |   \[   |
    |  \]  |   ]    |           |  \\\\]  |   \]   |
    •------•--------*           •---------•--------*
    |      |        |           |         |        |
    |  \\  |   \    |           |  \\\\   |   \\   |
    |  \*  |   *    |           |  \\\*   |   \*   |
    |  \+  |   +    |           |  \\\+   |   \+   |
    |  \#  |   #    |           |  \\\#   |   \#   |
    |  \-  |   -    |           |  \\\-   |   \-   |
    |  \=  |   =    |           |  \\\=   |   \=   |
    |  \>  |   >    |           |  \\\>   |   \>   |
    •------•--------*           •---------•--------*
    |  {   |   {    |           |  \\{    |   \{   |
    |  }   |   }    |           |  \\}    |   \}   |
    |  (   |   (    |           |  \\(    |   \(   |
    |  )   |   )    |           |  \\)    |   \)   |
    |  ^   |   ^    |           |  \\^    |   \^   |
    |  $   |   $    |           |  \\$    |   \$   |
    |  .   |   .    |           |  \\.    |   \.   |
    |  |   |   |    |           |  \\|    |   \|   |
    |  !   |   !    |           |  \\!    |   \!   |
    |  "   |   "    |           |  \\"    |   \"   |
    |  %   |   %    |           |  \\%    |   \%   |
    |  &   |   &    |           |  \\&    |   \&   |
    |  '   |   '    |           |  \\'    |   \'   |
    |  ,   |   ,    |           |  \\,    |   \,   |
    |  /   |   /    |           |  \\/    |   \/   |
    |  ;   |   ;    |           |  \\;    |   \;   |
    |  <   |   <    |           |  \\<    |   \<   |
    |  ?   |   ?    |           |  \\?    |   \?   |
    |  @   |   @    |           |  \\@    |   \@   |
    |  _   |   _    |           |  \\_    |   \_   |
    |  `   |   `    |           |  \\`    |   \`   |
    •------•--------•           •---------•--------• 
    
    - IF inside a CODE SPAN zone `....` or a CODE BLOCK zone  ~~~....~~~~  /  ```....``` or in text INDENTED with FOUR spaces :
    
    •------•--------*           •--------•--------*
    | Type | To get |           |  Type  | To get |
    •------•--------*           •--------•--------*
    |  [   |   [    |           |  \\[  |   \[    |
    |  ]   |   ]    |           |  \\]  |   \]    |
    •------•--------•           •--------•--------•
    |  \   |   \    |           |   \\   |   \\   |
    |  *   |   *    |           |   \*   |   \*   |
    |  +   |   +    |           |   \+   |   \+   |
    |  {   |   {    |           |   \{   |   \{   |
    |  }   |   }    |           |   \}   |   \}   |
    |  (   |   (    |           |   \(   |   \(   |
    |  )   |   )    |           |   \)   |   \)   |
    |  ^   |   ^    |           |   \^   |   \^   |
    |  $   |   $    |           |   \$   |   \$   |
    |  .   |   .    |           |   \.   |   \.   |
    |  |   |   |    |           |   \|   |   \|   |
    |  !   |   !    |           |   \!   |   \!   |
    |  "   |   "    |           |   \"   |   \"   |
    |  #   |   #    |           |   \#   |   \#   |
    |  %   |   %    |           |   \%   |   \%   |
    |  &   |   &    |           |   \&   |   \&   |
    |  '   |   '    |           |   \'   |   \'   |
    |  ,   |   ,    |           |   \,   |   \,   |
    |  -   |   -    |           |   \-   |   \-   |
    |  /   |   /    |           |   \/   |   \/   |
    |  :   |   :    |           |   \:   |   \:   |
    |  ;   |   ;    |           |   \;   |   \;   |
    |  <   |   <    |           |   \<   |   \<   |
    |  =   |   =    |           |   \=   |   \=   |
    |  >   |   >    |           |   \>   |   \>   |
    |  ?   |   ?    |           |   \?   |   \?   |
    |  @   |   @    |           |   \@   |   \@   |
    |  _   |   _    |           |   \_   |   \_   |
    |  `   |   `    |           |   \`   |   \`   |
    •------•--------•           •--------•--------•
    
    
    Remark : If a single BACK-TICK character must be inserted in a CODE SPAN zone, use the following THREE-lines syntax :
    
    •------•--------*           •--------•--------*
    | Type | To get |           |  Type  | To get |
    •------•--------*           •--------•--------*
    | **`` |        |           |  **``  |        |
    |  `   |   `    |           |  \`    |  \`    |
    | ``** |        |           |  ``**  |        |
    •------•--------•           •--------•--------•
    

    Best Regards,

    guy038