Notepad++ replace all the strings between two characters.



  • Hello, I using notepad++ regex to replace strings. I try to replace string between two characters .

    example:
    Here’s source text.
    a,a,a,b,b,b,b,a,a,a,a,c,c,c
    a,a,a,b,b,b,b,a,a,a,a,f,f,f

    result
    a,a,a,b,b,b,b,e,e,e,e,c,c,c
    a,a,a,b,b,b,b,a,a,a,a,f,f,f

    All the characters of ‘a’ between ‘b’ and ‘c’ will be replace to ‘e’.

    Can anybody give advice?



  • Can anybody give advice?

    Short answer: Yes.
    Long answer: Search for ‘b,a,a,a,a,c’ and replace with ‘b,e,e,e,e,c’, no regular expression required.

    When this is not the answer you were expecting you have to be more precise in formulating your question.



  • Hello, Shakespeare Willian,

    If I understand you correctly, starting with the example text, below :

                _____               ___                                     _______ 
    a,b,b,d,e,b,a,a,a,c,c,a,a,d,d,b,a,a,c,e,e,a,a,c,c,b,b,a,a,a,f,f,a,a,b,b,a,a,a,a,c,c
    

    you would like to change any a letter, located under an underscore, by the e letter ! Wouldn’t you ?

    I was not able to obtain this action with a single S/R, but, luckily, I found out a solution which implies two regex S/R !

    REMARKS :

    • We need two characters, not used yet, in the current file, to stand for the boundaries of the zone when any a letter will be changed be an e letter. I chose the < and > characters, but you may choose any other non-letter characters !

    • I do not consider the case where the opening boundary is on a line and the ending one, on an other line, further on !


    • FIRST S/R :

    SEARCH b([^b\r\n]+?)c

    REPLACE <\1>

    • Click on the Replace All button

    You should get the new text below :

    a,b,b,d,e,<,a,a,a,>,c,a,a,d,d,<,a,a,>,e,e,a,a,c,c,b,b,a,a,a,f,f,a,a,b,<,a,a,a,a,>,c
    
    • SECOND S/R :

    SEARCH (a)(?=[^<]*>)|(<)|(>)

    REPLACE (?1e)(?2b)(?3c)

    • Click on the Replace All button

    -You should get, now, the final text, below :

    a,b,b,d,e,b,e,e,e,c,c,a,a,d,d,b,e,e,c,e,e,a,a,c,c,b,b,a,a,a,f,f,a,a,b,b,e,e,e,e,c,c
    

    Let’s use, now, a “real” example ! This text, below, is part of the Notepad++ license.txt file

    2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions:
    
    
    a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change.
    
    b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License.
    

    After the FIRST S/R, we obtain :

    2. You may modify your copy or copies of the Program or any portion of it, thus forming a work <ased on the Program, and >opy and distri<ute su>h modifications or work under the terms of Section 1 a<ove, provided that you also meet all of these >onditions:
    
    
    a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change.
    
    <) You must >ause any work that you distribute or pu<lish, that in whole or in part >ontains or is derived from the Program or any part thereof, to <e li>ensed as a whole at no charge to all third parties under the terms of this License.
    

    After the SECOND S/R, we obtain :

    2. You may modify your copy or copies of the Program or any portion of it, thus forming a work besed on the Progrem, end copy and distribute such modifications or work under the terms of Section 1 above, provided thet you elso meet ell of these conditions:
    
    
    a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change.
    
    b) You must cause any work that you distribute or publish, thet in whole or in pert contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License.
    

    Notes :

    • The first regex, b([^b\r\n]+?)c tries to match any non null shortest range of characters, not containing any b letter nor EOL characters, between a b and a c letters and, just, replaces the boundaries b and c, by the < and > characters

    • Then, the second regex (a)(?=[^<]*>)|(<)|(>), matches :

      • Any letter a, if the condition that it could be, possibly, followed by characters, different from the <, till an ending > character, is True

      • Any boundary < or > ( the 2nd and 3rd alternatives )

    • BTW, in the negative square brackets class [^<], we don’t need to include the EOL characters \r\n, anymore, as we know, from the FIRST S/R, that the angle brackets range <........> is, all, located in a single line !

    • These characters, to match, are enclosed in round parenthesis and stored, respectively, as groups 1, 2 and 3

    • So, in the regex replacement (?1e)(?2b)(?3c), as soon as the group, 1, 2, OR 3 exists, the regex engine replaces, accordingly, the searched character a, < or >, by the letter e, b or c !

    Best Regards,

    guy038



  • @guy038
    Thanks for your replay, your solution works. That’s the exactly answer what I want.


Log in to reply