Community
    • Login

    How to replace text at a special place in special rows?

    Scheduled Pinned Locked Moved General Discussion
    22 Posts 7 Posters 1.2k 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.
    • EkopalypseE
      Ekopalypse
      last edited by

      I can’t seem to get the \g notation to work either.
      replace with:\g{12}2
      results in g{12}2

      PeterJonesP 1 Reply Last reply Reply Quote 1
      • PeterJonesP
        PeterJones @Ekopalypse
        last edited by PeterJones

        @Ekopalypse said in How to replace text at a special place in special rows?:

        what we could do in addition is
        replace with:($12)2

        Equivalently: replace with: ${12}2

        I can’t seem to get the \g notation to work either.
        replace with:\g{12}2

        In Boost, \g-notation is only listed in the SEARCH section, not in the REPLACE section (fixed typo).

        1 Reply Last reply Reply Quote 2
        • PeterJonesP
          PeterJones @Ekopalypse
          last edited by

          @Ekopalypse said in How to replace text at a special place in special rows?:

          \g notation

          To confirm: I can successfully search 0123456789ABCDEFB using the regex (.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)\g12, and it matches (because the 17th character matches the 12th backref)

          1 Reply Last reply Reply Quote 2
          • Alan KilbornA
            Alan Kilborn @Ekopalypse
            last edited by Alan Kilborn

            @Ekopalypse

            Haha, well, that’s why I said “fairly” certain.

            Actually, I cheated: Long ago I read about RegexBuddy here on the Community (at least I think it was here), and purchased a license. It has proved invaluable.

            Here’s what it told me for this case:

            f46cfd71-179a-448b-8be0-0581dc05baad-image.png

            I should have cited RB a few minutes ago when I posted, but I wanted to see if there was agreement/disagreement first.

            Very rarely have I found any discrepancies between RB and N++, but this may be one of those cases.

            It is interesting that RB doesn’t say “Insert the character string 22 literally” in the second and third lines of its output, but breaks it into 2 parts…hmmm…

            EkopalypseE 1 Reply Last reply Reply Quote 3
            • EkopalypseE
              Ekopalypse @Alan Kilborn
              last edited by

              @Alan-Kilborn

              maybe that is implementation detail (!?)

              1 Reply Last reply Reply Quote 1
              • ErwinSchmidt17E
                ErwinSchmidt17 @ErwinSchmidt17
                last edited by

                Thank you for all your answers.

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

                  Hello, @ErwinSchmidt17, @terry-r, @ekopalypse, @peterjones, @alan-kilborn and All,

                  Sorry, to be late as I’m on a family vacation right now, for the better part of August ;-))


                  Quickly, about solutions to @ErwinSchmidt17’s problem, I would say :

                  SEARCH (?-s)(^.+projectID:1234.+pf2|\G).*?\K11(?=.*pf3)

                  REPLACE 22

                  Thus, the test data, below, containing 4 names 11.png, in the pf2 section :

                  {name:11.png,filename:c:\img\11\11.png,projectID:1234},pf1:{spname:11.png,spfilename:11.png},pf2:{spname:11.png,spfilename:11.png,bla_blah:11.png,test:11.png},pf3:{spname:11.png,spfilename:11.png},pf4:{...}
                  

                  would be changed as :

                  {name:11.png,filename:c:\img\11\11.png,projectID:1234},pf1:{spname:11.png,spfilename:11.png},pf2:{spname:22.png,spfilename:22.png,bla_blah:22.png,test:22.png},pf3:{spname:11.png,spfilename:11.png},pf4:{...}
                  

                  Now, about the different syntaxes, related to groups, back-references and subroutine calls, I did some tests and here are my conclusions, not definitive, of course :

                  In search regexes, the possible syntaxes, with Boost regex library, are :

                  • Unnamed group is defined with surrounding parentheses : (.....)

                  • Named group is defined with the one of the syntaxes :

                    • (?<Name>.....)

                    • (?'Name'.....)

                  • Absolute back-reference, to an unnamed group N, is defined with one of the syntaxes :

                    • \N    ( with 1 <= N <= 9 )

                    • \gN    \g{N}    \g<N>    \g'N'    ( with 1 <= N <= Max )

                    • \kN    \k{N}    \k<N>    \k'N'    ( with 1 <= N <= Max )

                  • Relative back-reference, to an unnamed group X, is defined with one of the syntaxes :

                    • \g-X    \g{-X}    \g<-X>    \g'-X'    ( with 1 < X <= Max )

                    • \k-X    \k{-X}    \k<-X>    \k'-X'    ( with 1 < X <= Max )

                  • Absolute subroutine call, to an unnamed group N, is defined with the syntax :

                    • (?N)    ( with 0 <= N < Max )
                  • Relative subroutine call, to an unnamed group of relative number X, is defined with one of the syntaxes :

                    • (?-X)    ( with 1 < X <= Max )

                    • (?+X)    ( with 1 < X <= Max )

                  • Absolute back-reference, to a named group Name, is defined with one of the syntaxes :

                    • \g{Name}    \g<Name>    \g'Name'

                    • \k{Name}    \k<Name>    \k'Name'

                  • Absolute subroutine call, to a named group Name, is defined with one of the syntaxes :

                    • (?&Nom)

                    • (?P>Nom)


                  Remarks :

                  • For all the relative syntaxes above, the Max value is the greatest group of the overall regex

                  • For all the absolute syntaxes, I suppose that the Max value is 2,147,483,647, as it’s the same value in replacement, too !

                  • The names of named groups are word characters, non beginning with a digit

                  • The (?0) is a subroutine call to the overall regex and is, implicitly, a recursive subroutine call !

                  Summary example :

                  To find a four-letters word palindrome, you can use, either, one of these 23 syntaxes :

                  \b(\w)(\w)\2\1\b

                  \b(\w)(\w)\g2\g1\b
                  \b(\w)(\w)\g{2}\g{1}\b
                  \b(\w)(\w)\g<2>\g<1>\b
                  \b(\w)(\w)\g'2'\g'1'\b

                  \b(\w)(\w)\k2\k1\b
                  \b(\w)(\w)\k{2}\k{1}\b
                  \b(\w)(\w)\k<2>\k<1>\b
                  \b(\w)(\w)\k'2'\k'1'\b

                  \b(\w)(\w)\g-1\g-2\b
                  \b(\w)(\w)\g{-1}\g{-2}\b
                  \b(\w)(\w)\g<-1>\g<-2>\b
                  \b(\w)(\w)\g'-1'\g'-2'\b

                  \b(\w)(\w)\k-1\k-2\b
                  \b(\w)(\w)\k{-1}\k{-2}\b
                  \b(\w)(\w)\k<-1>\k<-2>\b
                  \b(\w)(\w)\k'-1'\k'-2'\b

                  \b(?<First>\w)(?'Second'\w)\g{Second}\g{First}\b
                  \b(?<First>\w)(?'Second'\w)\g<Second>\g<First>\b
                  \b(?<First>\w)(?'Second'\w)\g'Second'\g'First'\b

                  \b(?'First'\w)(?<Second>\w)\k{Second}\k{First}\b
                  \b(?'First'\w)(?<Second>\w)\k<Second>\k<First>\b
                  \b(?'First'\w)(?<Second>\w)\k'Second'\k'First'\b

                  Test them against this text :

                  adda – a type of lizard
                  Adda – a river in Italy; a river in Wales
                  Anna – a girl’s name
                  Beeb – an informal name for the BBC
                  boob – a blunder; a breast
                  deed – various common meanings
                  goog – an egg (Australian slang)
                  immi – a Swiss unit of volume
                  keek – to peep
                  kook – a crazy person
                  naan – a type of Indian bread
                  noon – midday
                  Otto - a proper name
                  peep – various common meanings
                  poop – a raised deck at the stern of a ship; various other meanings
                  toot – the sound made by a horn or whistle
                  

                  Now, as a subroutine call is, basically, a reference to the regex itself, included in a group and NOT the last value of this group like in back-references, the 5 following syntaxes are strictly equivalent to the simple regex \b\w{4}\b and looks for a four-letters word :

                  \b(\w)(\w)(?2)(?1)\b
                  \b(\w)(\w)(?-1)(?-2)\b
                  \b(?+2)(?+1)(\w)(\w)\b

                  \b(?<First>\w)(?'Second'\w)(?&Second)(?&First)\b
                  \b(?'First'\w)(?<Second>\w)(?P>Second)(?P>First)\b

                  Test them, again, on the same sample text, above !

                  Important :

                  • All the syntaxes, above, are valid in search part ONLY !

                  • Because of the multiple equivalent syntaxes, for groups, back-references and subroutine calls, it is useful to define, for search regexes, a single, minimal syntax, covering the majority of cases :

                  Hence, the table, below, with my preferences :

                      •============================•=============================•===================•====================•
                      |           GROUP            |          REFERENCE          |  ABSOLUTE number  |  RELATIVE number   |
                      •============================•=============================•===================•====================•
                      |                            |       BACK-REFERENCE        |  \N   or   \g{N}  |       \g{-X}       |
                      |  (.....)          UNNAMED  |                             |                   |                    |
                      |                            |       SUBROUTINE CALL       |       (?N)        |  (?-X)  or  (?-X)  |
                      •----------------------------•-----------------------------•-------------------•--------------------•
                      |                            |       BACK-REFERENCE        |     \g<Name>      |        N/A         |
                      |  (?<Name>.....)     NAMED  |                             |                   |                    |
                      |                            |       SUBROUTINE CALL       |     (?&Name)      |        N/A         |
                      •============================•=============================•===================•====================•
                  

                  In replacement regexes, , with Boost regex library, you can use the following syntaxes :

                  • Absolute reference, to an unnamed group N, is defined with either :

                    • \N    ( with 1 <= N <= 9 )

                    • $N    ( with 0 <= N <= 2,147,483,647 )

                    • ${N}    ( with 0 <= N <= 2,147,483,647 )

                  • Absolute reference, to an named group Name, is defined with the syntax :

                    • $+{Name}

                  Remarks :

                  • The $0 or $& syntaxes refer to the overall regex, itself

                  • If number N is superior to the number of back-references, in the search regex, these syntaxes return an empty string

                  • If a named reference $+{name} does not exist in search regex, it also returns an empty string

                  • If, in the replacement regex, a digit follows a $N syntax, it’s preferable to use the ${N} form !

                  • The $00...00N and ${00...00N} syntaxes are equivalent to, respectively, the $N and ${N} syntaxes

                  • So, the single minimal syntaxes, in replacement, seems to be :

                      •============================•==================•===================•
                      |           GROUP            |     REFERENCE    |  ABSOLUTE number  |
                      •============================•==================•===================•
                      |  (.....)          UNNAMED  |  BACK-REFERENCE  |       ${N}        |
                      •----------------------------•------------------•-------------------•
                      |  (?<Name>.....)     NAMED  |  BACK-REFERENCE  |     $+{Name}      |
                      •============================•==================•===================•
                  

                  Best Regards,

                  guy038

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

                    Hi, All,

                    Out of curiosity, do you know how I could determine that the maximum number of group is 2,147,483,647 ?

                    Well, I began the test using this simple regex S/R :

                    SEARCH (?-s).

                    REPLACE --${300}--

                    When replacing a single character, it returns the string ----. So, the S/R seemed valid and, as the group 300 did not exist, it just wrote the empty string as replacement of this group.

                    Then, I, successively, changed the replacement zone with :

                    • --${3000}--    =>    ----

                    • --${30000}--    =>    ----

                    • --${3000000000}--    =>    --$3000000000}--

                    As I suspected that the limit should have a relation to powers of 2, I searched for the largest power of 2, below 3,000,000, which is 2^31 = 2,147,483,647 !

                    Indeed :

                    SEARCH (?-s).

                    REPLACE --${2147483647}--

                    => The ---- output

                    and :

                    SEARCH (?-s).

                    REPLACE --${2147483648}--

                    => The --${2147483648}-- output

                    Of course, I do understand that this limit is quite theoretical ! Just imagine a regex containing 2,147,483,647 different groups… Brrrrr

                    Best Regards,

                    guy038

                    EkopalypseE 1 Reply Last reply Reply Quote 2
                    • EkopalypseE
                      Ekopalypse @guy038
                      last edited by

                      @guy038 - sounds like a 32bit integer limitation.

                      Alan KilbornA 1 Reply Last reply Reply Quote 1
                      • Alan KilbornA
                        Alan Kilborn @Ekopalypse
                        last edited by

                        @Ekopalypse

                        sounds like a 32bit integer limitation.

                        Or an implementation detail. ;-)
                        BTW, trying this in RegexBuddy, it reports “group 2147483647” but if you go one higher it reports “group -2147483648”.

                        EkopalypseE 1 Reply Last reply Reply Quote 2
                        • EkopalypseE
                          Ekopalypse @Alan Kilborn
                          last edited by

                          @Alan-Kilborn

                          :-D

                          1 Reply Last reply Reply Quote 1
                          • Makwana PrahladM
                            Makwana Prahlad Banned
                            last edited by

                            Hello,@ErwinSchmidt17

                            Follow this step,To How to replace text at a special place in special rows?

                            Step 1: Press Ctrl+H to bring up the Find/Replace Dialog.
                            Step 2: Choose the Regular expression option near the bottom of the dialog.

                            To add a word, such as test, at the beginning of each line:
                            Step 1: Type ^ in the Find what textbox
                            Step 2: Type test in the Replace with textbox
                            Step 3: Place cursor in the first line of the file to ensure all lines are affected
                            Step 4: Click Replace All button

                            To add a word, such as test, at the end of each line:
                            Step 1: Type $ in the Find what textbox
                            Step 2: Type test in the Replace with textbox
                            Step 3: Place cursor in the first line of the file to ensure all lines are affected
                            Step 4: Click Replace All button

                            I hope this information will be useful.
                            Thank you.

                            Alan KilbornA 1 Reply Last reply Reply Quote -1
                            • Alan KilbornA
                              Alan Kilborn @Makwana Prahlad
                              last edited by

                              @Makwana-Prahlad

                              How is that relevant?
                              You provided a solution for 2 things that were not even asked for.

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