Community
    • Login

    Select bookmarked lines

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    26 Posts 5 Posters 21.1k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • guy038G
      guy038
      last edited by guy038

      Hello, abuali huma,

      You didn’t say if the regex must keep the Line 2 unchanged or if you want to wipe out this line !

      Anyway, here are the appropriate regexes for each case :

      • 1) If line 2 is unchanged :

      From the original text :

      FGHI;Ax#&;Dx#&
      BCDE
      

      the S/R, below :

      SEARCH (?-si)^(FGHI)(.*)(?=\R(BCDE))

      REPLACE \3\2\1

      would give the result :

      BCDE;Ax#&;Dx#&FGHI
      BCDE
      

      NOTES :

      • The first part (?-si) forces the dot ( . ) to match standard characters, only, and the regex engine to work, in a NON-insensitive way

      • The \R form represents any kind of EOL characters (\r\n ), (\n ) or (\r )

      • The (?=\R(BCDE)) syntax is called a positive look-ahead, that is to say a condition which must be verified to valid the overall regex, but which is never part of the final match. So the condition is “Does it exist, at the end of line 1, some EOL character(s), followed by the string BCDE ?”. The string BCDE is stored in group 3


      • 2) If line 2 must be deleted, too :

      From the original text :

      FGHI;Ax#&;Dx#&
      BCDE
      

      The second S/R :

      SEARCH (?-si)^(FGHI)(.*)\R(BCDE)

      REPLACE \3\2\1

      would give the final text :

      BCDE;Ax#&;Dx#&FGHI
      

      Best Regards,

      guy038

      abuali humaA 2 Replies Last reply Reply Quote 0
      • abuali humaA
        abuali huma @guy038
        last edited by abuali huma

        @guy038 said:

        Hello, abuali huma,

        You didn’t say if the regex must keep the Line 2 unchanged or if you want to wipe out this line !

        Anyway, here are the appropriate regexes for each case :

        • 1) If line 2 is unchanged :

        From the original text :

        FGHI;Ax#&;Dx#&
        BCDE
        

        the S/R, below :

        SEARCH (?-si)^(FGHI)(.*)(?=\R(BCDE))

        REPLACE \3\2\1

        would give the result :

        BCDE;Ax#&;Dx#&FGHI
        BCDE
        

        NOTES :

        • The first part (?-si) forces the dot ( . ) to match standard characters, only, and the regex engine to work, in a NON-insensitive way

        • The \R form represents any kind of EOL characters (\r\n ), (\n ) or (\r )

        • The (?=\R(BCDE)) syntax is called a positive look-ahead, that is to say a condition which must be verified to valid the overall regex, but which is never part of the final match. So the condition is “Does it exist, at the end of line 1, some EOL character(s), followed by the string BCDE ?”. The string BCDE is stored in group 3


        • 2) If line 2 must be deleted, too :

        From the original text :

        FGHI;Ax#&;Dx#&
        BCDE
        

        The second S/R :

        SEARCH (?-si)^(FGHI)(.*)\R(BCDE)

        REPLACE \3\2\1

        would give the final text :

        BCDE;Ax#&;Dx#&FGHI
        

        Best Regards,

        guy038

        What if it is different text?
        Example one
        Line#1 こんにちは;Ax#&;Dx#&
        Line#2 行く
        Result
        Line#1 行く ;Ax#&;Dx#&こんにちは

        Example two
        Line#18 細かい ;Ax#&;Dx#&
        Line#19 自分を愛する
        Result
        Line#18 自分を愛する ;Ax#&;Dx#& 細かい

        and so on, and yes 2nd line is supposed to be deleted

        1 Reply Last reply Reply Quote 0
        • guy038G
          guy038
          last edited by guy038

          Hi, abuali huma,

          Sorry, I was absent for a while, because of an virus analysis on my laptop ! But, please be quiet : absolutely NO connection with our NodeBB site nor our discussion, too :-))

          For a general regex, no problem at all ! I just interpreted that :

          • Group 1, beginning the first line, is supposed to contain ideographic characters, ONLY

          • Group 2 represents any range, after the last ideographic character, in the first line, till the end of the line

          • Group 3, standing for the second line, is supposed, also, to contain ideographic characters, ONLY

          Remark :

          Some additional space characters, NOT present in the original text, seem included in the replacement text, of your two examples :

          • Before the first semicolon, in your first example

          • After the ampersand character, in your second example

          From my hypotheses, any space character would be stored in group 2, of course !. In addition, you may separate, in replacement, the three groups with a space character, as well !


          So, a general S/R would be :

          SEARCH (?-si)^([\x{3000}-\x{9faf}]+)(.*)\R([\x{3000}-\x{9faf}]+)

          REPLACE \3\2\1

          Notes :

          • The part [\x{3000}-\x{9faf}]+ tries to match the largest, non empty, range of ideographic characters, from beginning of a line ( ^ ), stored in group 1 OR after an EOL character (\R ), stored in group 3

          • During replacement, these three groups, on two consecutive lines, are, just, re-written, in a single line, into a different order !

          • As said above, the replacement regex may be changed into \3 \2 \1

          Cheers,

          guy038

          hu maH 1 Reply Last reply Reply Quote 0
          • hu maH
            hu ma @guy038
            last edited by

            @guy038 said:

            Hi, abuali huma,

            Sorry, I was absent for a while, because of an virus analysis on my laptop ! But, please be quiet : absolutely NO connection with our NodeBB site nor our discussion, too :-))

            For a general regex, no problem at all ! I just interpreted that :

            • Group 1, beginning the first line, is supposed to contain ideographic characters, ONLY

            • Group 2 represents any range, after the last ideographic character, in the first line, till the end of the line

            • Group 3, standing for the second line, is supposed, also, to contain ideographic characters, ONLY

            Remark :

            Some additional space characters, NOT present in the original text, seem included in the replacement text, of your two examples :

            • Before the first semicolon, in your first example

            • After the ampersand character, in your second example

            From my hypotheses, any space character would be stored in group 2, of course !. In addition, you may separate, in replacement, the three groups with a space character, as well !


            So, a general S/R would be :

            SEARCH (?-si)^([\x{3000}-\x{9faf}]+)(.*)\R([\x{3000}-\x{9faf}]+)

            REPLACE \3\2\1

            Notes :

            • The part [\x{3000}-\x{9faf}]+ tries to match the largest, non empty, range of ideographic characters, from beginning of a line ( ^ ), stored in group 1 OR after an EOL character (\R ), stored in group 3

            • During replacement, these three groups, on two consecutive lines, are, just, re-written, in a single line, into a different order !

            • As said above, the replacement regex may be changed into \3 \2 \1

            Cheers,

            guy038

            Thanks mate, I will give it a try once I get back on my pc after a while…

            But as I understand from you, that is in group 1&3, if the line had mixed character set, the regex will not apply to that line ?

            1 Reply Last reply Reply Quote 0
            • guy038G
              guy038
              last edited by

              abuali huma,

              Seemingly, you consider in the original first line, two parts :

              • The first part , which, after replacement, will be moved to the end of this first line

              • The second part, which, after replacement, will follow the contents of the deleted second line

              I, just, supposed that the limit, between these two parts, was the one between ideographic and NON-ideographic characters

              However, any kind of limit may be considered. For instance, if you prefer, we may suppose that group 1 is all the characters of the first line, till a first semicolon or… anything else ! Just tell me which criterion to choose :-))

              Cheers,

              guy038

              abuali humaA 1 Reply Last reply Reply Quote 0
              • abuali humaA
                abuali huma @guy038
                last edited by abuali huma

                @guy038 said:

                abuali huma,

                Seemingly, you consider in the original first line, two parts :

                • The first part , which, after replacement, will be moved to the end of this first line

                • The second part, which, after replacement, will follow the contents of the deleted second line

                I, just, supposed that the limit, between these two parts, was the one between ideographic and NON-ideographic characters

                However, any kind of limit may be considered. For instance, if you prefer, we may suppose that group 1 is all the characters of the first line, till a first semicolon or… anything else ! Just tell me which criterion to choose :-))

                Cheers,

                guy038

                Well let’s see,
                group one should be all characters until the first semicolon
                group two from the semicolon till line ends
                group three should be all characters in the line

                and there’s some lines “minoraty” that contains 5 or 7 groups, but I think I could just duplicate the process.

                1 Reply Last reply Reply Quote 0
                • guy038G
                  guy038
                  last edited by guy038

                  Hi, abuali huma,

                  OK.! So, the general S/R becomes :

                  SEARCH (?-si)^(.+?)(;.*)\R(.+)

                  REPLACE \3\2\1

                  NOTES :

                  • The group 2, ( (;.*) ), begins, as expected, with a semicolon, followed by any range, even empty, of standard character(s) ( Remember that the quantifier * is the short form for {0,} ! )

                  • The group 1, ( (.+?) ), after the location beginning of line, looks for the shortest, NON-empty, range of standard character, till a semicolon.

                  • Note that the form .+?, equivalent to .{1,}? is a lazy quantifier, which forces the regex engine to find the MINIMUM of standard characters till a semicolon => So, in your previous first example, group 1 is equal to the string こんにちは

                  • On the contrary, if we have used the greedy quantifier, .+, equivalent to .{1,}, it would have forced the regex engine to find the MAXIMUM of standard characters, till a semicolon. So, in your previous first example, group 1 would have been equal to the string こんにちは;Ax#&, before the second semicolon !

                  • The group 3, ( (.+) ), after the EOL character(s) of the first line, represents any NON-empty range of standard characters, of the second line

                  • Remember that, as each group contains standard characters, they may, also, begins and/or ends with some space or tabulation character(s), except for group 2, which must begin with a semicolon


                  BTW, to know the meaning of the different quantifiers, just see my post to Casey, below :

                  https://notepad-plus-plus.org/community/topic/12905/how-to-remove-duplicate-row-in-find-result/4NON-empty

                  Best Regards,

                  guy038

                  hu maH 1 Reply Last reply Reply Quote 1
                  • hu maH
                    hu ma @guy038
                    last edited by

                    @guy038 said:

                    Hi, abuali huma,

                    OK.! So, the general S/R becomes :

                    SEARCH (?-si)^(.+?)(;.*)\R(.+)

                    REPLACE \3\2\1

                    NOTES :

                    • The group 2, ( (;.*) ), begins, as expected, with a semicolon, followed by any range, even empty, of standard character(s) ( Remember that the quantifier * is the short form for {0,} ! )

                    • The group 1, ( (.+?) ), after the location beginning of line, looks for the shortest, NON-empty, range of standard character, till a semicolon.

                    • Note that the form .+?, equivalent to .{1,}? is a lazy quantifier, which forces the regex engine to find the MINIMUM of standard characters till a semicolon => So, in your previous first example, group 1 is equal to the string こんにちは

                    • On the contrary, if we have used the greedy quantifier, .+, equivalent to .{1,}, it would have forced the regex engine to find the MAXIMUM of standard characters, till a semicolon. So, in your previous first example, group 1 would have been equal to the string こんにちは;Ax#&, before the second semicolon !

                    • The group 3, ( (.+) ), after the EOL character(s) of the first line, represents any NON-empty range of standard characters, of the second line

                    • Remember that, as each group contains standard characters, they may, also, begins and/or ends with some space or tabulation character(s), except for group 2, which must begin with a semicolon


                    BTW, to know the meaning of the different quantifiers, just see my post to Casey, below :

                    https://notepad-plus-plus.org/community/topic/12905/how-to-remove-duplicate-row-in-find-result/4NON-empty

                    Best Regards,

                    guy038

                    Thanks, that is perfect one, it saved me alot of time

                    1 Reply Last reply Reply Quote 0
                    • abuali humaA
                      abuali huma @guy038
                      last edited by

                      Back again,
                      this time I want to swap two sentences or three separated by a symbol.

                      example1
                      Original

                       this is text number two|This is text number one
                      

                      Result

                       This is text number one|this is text number two
                      

                      Example2

                       this is text number three|this is text number two|This is text number one
                      

                      Result

                       This is text number one|this is text number two|this is text number three
                      
                      1 Reply Last reply Reply Quote 0
                      • guy038G
                        guy038
                        last edited by

                        Hi, abuali huma,

                        Not very difficult to achieve !


                        EXAMPLE 1 :

                        SEARCH ^(.+)\|(.+)

                        REPLACE \2|\1

                        Notes :

                        • This first regex separates the two parts of text, before and after the symbol | , which must be escaped ( \| ) as this symbol, in search regexes, stands for the normal alternation symbol !

                        • The part, before the symbol, is stored as group 1 and the part, located after, is stored as group 2

                        • In replacement, we just reverse the order of these two groups


                        EXAMPLE 2

                        SEARCH ^(.+?)\|(.+)\|(.+)

                        REPLACE \3|\2|\1

                        Notes :

                        • This second regex separates the three parts of text, created by the symbol |, which are stored as group 1, group 2 and group 3

                        • The first part ^(.+?)\|, tries to match, from beginning of line ( ^) , the shortest non-null range of characters, till the FIRST separation symbol ( \| ), because of the lazy quantifier ( +?)

                        • Note that if we would have written this first part ^(.+)\|, it would have matched the string this is text number three|this is text number two. Indeed, due to the greedy quantifier ( + ), it would have matched the tallest non-null range of characters, till the SECOND separation symbol !

                        • Then for the two remaining parts, of the regex, (.+)\|(.+) just refer to the previous regex for explanations

                        • In replacement, we just perform a permutation of these three groups

                        Cheers,

                        guy038

                        1 Reply Last reply Reply Quote 1
                        • First post
                          Last post
                        The Community of users of the Notepad++ text editor.
                        Powered by NodeBB | Contributors