Community
    • Login

    RegEx Help with Backreference I think

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    58 Posts 4 Posters 1.7k 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.
    • Robert Or Janet DiebelR
      Robert Or Janet Diebel @Robert Or Janet Diebel
      last edited by

      @Robert-Or-Janet-Diebel
      Thanx!
      That appears to have worked.
      Wishing you a great day.

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

        Hi, @robert-or-janet-diebel, @terry-r and All,

        Robert, I’m really sorry because I did a mistake regarding my second and main regex replacement : I mixed two different tests :-((

        The correct regex S/R was simply :

        • FIND (?-is)\\v (?=(?s:.+?^(Chapter \d+$)))

        • REPLACE $1:


        But, I agree that the @terry-r’s use of the Edit > Line Operations > Reverse Line Order, in order to get the Chapter name after its contents, is quite clever !

        So, if we use the @terry-r’s method and taking in account your further posts, the task is really simplified :

        • Run an Edit > Line Operations > Reverse Line order action

        • Perform the following regex S/R

          • FIND (?-is)\\v (?=(?s:.+?^(\u[\u\l -]+ \d+$)))

          • REPLACE $1:

        • Run again an Edit > Line Operations > Reverse Line order action


        I used the regex (?-i)\u[\u\l -]+ for the book’s name, just in case you have this kind of title : My very-Best-Novel !

        BR

        guy038

        Robert Or Janet DiebelR 2 Replies Last reply Reply Quote 1
        • Robert Or Janet DiebelR
          Robert Or Janet Diebel @guy038
          last edited by

          @guy038 Thanx very much. I am still working on this as the total project requires more changes.

          All help is appreciated.
          I am trying to do what I can myself, but am in over my head here.

          Robert Or Janet DiebelR 1 Reply Last reply Reply Quote 0
          • Robert Or Janet DiebelR
            Robert Or Janet Diebel @Robert Or Janet Diebel
            last edited by

            @Robert-Or-Janet-Diebel
            Adds Book name fine, but all chapters show as “1”

            Robert Or Janet DiebelR 1 Reply Last reply Reply Quote 0
            • Robert Or Janet DiebelR
              Robert Or Janet Diebel @Robert Or Janet Diebel
              last edited by

              @Robert-Or-Janet-Diebel
              Could be my fault. I am still testing

              Terry RT 1 Reply Last reply Reply Quote 0
              • Terry RT
                Terry R @Robert Or Janet Diebel
                last edited by

                @Robert-Or-Janet-Diebel
                Your last few posts aren’t referencing anybody. The @name you use is how to “speak” to a particular poster.

                The posts are also lacking in detail so it is difficult to know what your problem is.

                Suggest adding some examples showing before and after the change and what regex you used.

                Terry

                Robert Or Janet DiebelR 3 Replies Last reply Reply Quote 0
                • Robert Or Janet DiebelR
                  Robert Or Janet Diebel @Terry R
                  last edited by

                  @Terry-R
                  I have, a number of times, needed to grab a Value (usually a book name in this instance)
                  Then I need to fit it into a search as a parameter (maybe as a group) to be used in the Replace.

                  I believe you did that, but I am not sure how.
                  Could you expand a bit one the change you did from “Chapter” to BookName?

                  1 Reply Last reply Reply Quote 0
                  • Robert Or Janet DiebelR
                    Robert Or Janet Diebel @guy038
                    last edited by Robert Or Janet Diebel

                    @guy038
                    I have, a number of times, needed to grab a Value (usually a book name in this instance)
                    Then I need to fit it into a search as a parameter (maybe as a group) to be used in the Replace.

                    Your FIND did work well,
                    Thanx.

                    1 Reply Last reply Reply Quote 1
                    • Robert Or Janet DiebelR
                      Robert Or Janet Diebel @Terry R
                      last edited by

                      @Terry-R I am trying to put together an example.
                      Will post soon I hope.

                      1 Reply Last reply Reply Quote 0
                      • Robert Or Janet DiebelR
                        Robert Or Janet Diebel @Terry R
                        last edited by

                        @Terry-R Given:

                        \id GEN KUPUTUKANEW1, September 18, 2012, LCP LUVALE 
                        \h KUPUTUKA
                        \toc1 Mukanda waKuputuka
                        \toc2 Kuputuka
                        \toc3 Kupu
                        \mt2 MUKANDA WA
                        \mt1 KUPUTUKA
                        \mt2 MUKANDA WAKAVANGA ÁSONEKELE MOSE
                        \imt Kujimbula mukanda
                        \is Ásonekele mukanda
                        \ip Ou Mukanda waKuputuka vauvulukila Mose ngwavo iki
                        \ip Mose vamuvulukila kusoneka mikanda yosena yitanu 
                        \is Mwaka wakuusoneka
                        \ip Mwaka asonekele Mose uno mukanda wapwile mukachi 
                        \is Vihande vili mumukanda
                        \ip Mumukanda waKuputuka mukiko vasoneka haputukilile
                        \ip MuKuputuka mukiko aputukila Kalunga kulisolola om
                        \ip Havilongeselo-vyalushiko vitanu navitatu vize vya
                        \ip Vihande vyamuMukanda waKuputuka vyengila namumuka
                        \im Unjiho wakuvuluka Mukanda Kuputuka kupwa wamuchan
                        \is Vihanda vitanu vili mumukanda
                        \ili1 Kutanga vyamumelu navyahakaye (1:1—2:25)
                        \ili1 Hafumine kulikanga, nomu Kalunga ashikile ulwil
                        \ili1 Kulihandununa chambuto yavatu (Kena naAvele), n
                        \ili1 Kufuma halivaji, nakutwala kuMbavele (8:1—11:9)
                        \ili1 Kufuma haze Apalama vamusanyikile, nakutwala ku
                        \c 1
                        \s Kuputuka chakutanga vyuma
                        \q1
                        \v 1 \x + \xo 1:1\xt Yowa 1:1-2\x*Kalunga omu aputuki
                        \q1
                        \v 2 Vyahakaye vyapwile kanda visoloke, vyapwile vich
                        \q1 Milima yafwikile hamwambe.
                        \q1 Shipilitu\f + \fr 1:2 \ft Eli lizu shipilitu lyap
                        \s Kutanga mumakumbi atanu nalimwe
                        \p
                        \v 3 Jino \x + \xo 1:3\xt Samu 33:6; 2 Koli 4:6\x*Kal
                        \m
                        
                        

                        How would I grab the word following \toc 2{Space} and use it in the Replace?
                        FIND - \id.\toc2 (.?)\r\n.*?Kuputuka 1 - REPLACE - Kuputuka 1 - Remove Preamble
                        This works, but I have to enter the Book Name manually.
                        Thanx for all help.

                        Terry RT 1 Reply Last reply Reply Quote 0
                        • Terry RT
                          Terry R @Robert Or Janet Diebel
                          last edited by

                          @Robert-Or-Janet-Diebel said in RegEx Help with Backreference I think:

                          This works, but I have to enter the Book Name manually.

                          Well your example is what I’d call the “before” example. How about show the same data but with the changes you’d need to make. As for a book name, I don’t even know where in your example the book name is, it certainly isn’t obvious.

                          In fact the examples now are looking a lot more complicated than your initial example. That leads me to believe some of the earlier stuff was perhaps made up, and this is the real data. Whilst we are happy to help, I (personally) feel a bit used if it turns out the original solutions weren’t really needed.

                          If you have a “real” request, please lay it out. We’ve had other posters in the past that “beat about the bush” and only through lots of posts do we realise their actual problem wasn’t the one they initially started with. That is just wasting time and quite frankly can cause friction.

                          Terry

                          Robert Or Janet DiebelR 1 Reply Last reply Reply Quote 2
                          • Robert Or Janet DiebelR
                            Robert Or Janet Diebel @Terry R
                            last edited by

                            @Terry-R I am truly sorry about this.
                            There is no attempt to deceive, or use.
                            And I know you time is valuable by the depth of your responses.
                            I have a number of macros. They were being executed in a certain order. Your responses help me to think there could be a better way. Maybe doing them in a different order. I am experimenting with that, and will continue.
                            As stated earlier:
                            I have, a number of times, needed to grab a Value (usually a book name in this instance)
                            Then I need to fit it into a search as a parameter (maybe as a group) to be used in the Replace.

                            The book Name here is “Kuputuka”. I am trying to help some folks who speak the Luvale language.
                            I need to grab that word, and be able to use it.
                            I will continue to search.
                            Your help thus far has moved me forward in understanding some RegEx syntax.
                            If I can come up with a better example I will post it.
                            Again, sorry to have troubled you.
                            Your input has been very much appreciated.

                            Terry RT 1 Reply Last reply Reply Quote 0
                            • Terry RT
                              Terry R @Robert Or Janet Diebel
                              last edited by Terry R

                              @Robert-Or-Janet-Diebel said in RegEx Help with Backreference I think:

                              Your help thus far has moved me forward in understanding some RegEx syntax.

                              I think you are trying to run before you can walk. Learning Regex is like learning a computer language, albeit with a small number of defined “reserved words”. This is a common issue when we help out, of which we are well aware. Often we will try to describe what our solutions do. Sometimes we will point out the website regex101.com as entering the regex there, you will get a description (sometimes it doesn’t work as there are many flavours of regular expressions).

                              I also offer up rexegg.com as that can be a great tool as well. But as with any learning it is best to start with basics, much as building a house. Start with the foundations and work your way up.

                              It is good that you are trying to find your own way. Just be aware that we can help. Just be honest, show what you have tried, where it failed as you did in your first post. When asking questions give as much background information as possible and please include examples, both before and after the required changes.

                              One of your statements “I have, a number of times, needed to grab a Value (usually a book name in this instance). Then I need to fit it into a search as a parameter (maybe as a group) to be used in the Replace.” suggests to me that this is 2 separate steps. Bear in mind that regex does NOT store values across steps. If a regex cannot grab and then use that parameter within itself then the process will not work. At that point it might be necessary to go to a proper programming language. Notepad++ supports several, PythonScript and LuaScript being 2 of them.

                              Terry

                              Robert Or Janet DiebelR 1 Reply Last reply Reply Quote 2
                              • Robert Or Janet DiebelR
                                Robert Or Janet Diebel @Terry R
                                last edited by

                                @Terry-R Thanx Terry, I will keep trying.

                                1 Reply Last reply Reply Quote 0
                                • Robert Or Janet DiebelR
                                  Robert Or Janet Diebel @guy038
                                  last edited by

                                  @guy038 Your RegEx works great.

                                  (?-is)\\v (?=(?s:.+?^(\u[\u\l -]+ \d+$)))
                                  

                                  The BookName is the first word in the file so when line reversed it is the last.
                                  Could you explain how your RegEx grabs that last word and is able to use it?

                                  This project uses a number of MACROs. It would be helpful for me to understand how I can get the BookName from the file.
                                  As it is now, I need to alter the shortcuts.xml file too often.
                                  I have gotten the manual and am trying to figure it out but any help you could give would be much appreciate and might shorten my search substantually.

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

                                    Hello, @robert-or-janet-diebel ,

                                    Sorry, but I’ve been away since Friday on a ski course at the French resort of Val-Cenis ! Now I’m back and preparing my reply concerning my search/replacement, which I hope will contain all the necessary explanations !

                                    See you soon !

                                    Best Regards,

                                    guy038

                                    Robert Or Janet DiebelR 1 Reply Last reply Reply Quote 0
                                    • Robert Or Janet DiebelR
                                      Robert Or Janet Diebel @guy038
                                      last edited by

                                      @guy038 Excellent. Thanx so much.

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

                                        Hello, @robert-or-janet-diebel,

                                        IMPORTANT :

                                        After three days outdoors, I have a fresh perspective and I’ve simplified my regex S/R as below :

                                        • FIND (?s-i)\\v (?=.+?^(\u[\u\l -]+ \d+$))

                                        • REPLACE $1:


                                        First, given your first INPUT text, below :

                                        Chapter 1
                                        Chapter 1:1	Kukuputuka Lizu apwilengako, kah
                                        Chapter 1:2	Ikiye apwilenga naKalunga kukupu
                                        Chapter 1:3	Vyosena vavitengelele muli Ikiye
                                        Chapter 1:4	Muli Ikiye mwapwile Mwono, kaha 
                                        Chapter 1:5	Musana kana wamunyika mumilima, 
                                        Chapter 1:6	Kwapwile mutu vatumine kufuma ku
                                        Chapter 1:7	Ikiye ejile apwenga chinjiho wak
                                        Chapter 1:8	Ikiye kapwile Uze Musanako, oloz
                                        Chapter 1:9	Musana wamuchano apwile nakwiza 
                                        Chapter 1:10	Apwile hakaye, kaha kaye vakaten
                                        Chapter 1:11	Ejile kuvyenyi, kaha vaze venyi 
                                        Chapter 1:12	Oloze veshovo vamutambwile, vaze
                                        Chapter 1:13	vaze vene vásemuwile, keshi kuma
                                        Chapter 1:14	Jino Lizu alilingishile Ivene ku
                                        Chapter 2
                                        \v 1	Halikumbi lyamuchitatu kwapwile chil
                                        \v 2	Yesu nawa vamusanyikile natumbaji tw
                                        \v 3	Omu vinyo yahwile, naye alwezele Yes
                                        \v 4	Kaha Yesu amwambile ngwenyi, Ove mam
                                        \v 5	Naye alwezele vangamba ngwenyi, Ches
                                        \v 6	Jino kuze kwapwile milondo yamalolwa
                                        \v 7	Yesu avambile ngwenyi, Zalisenu milo
                                        \v 8	Avambile ngwenyi, Switenumo jino mum
                                        \v 9	Aze meya álumukile kupwa vinyo, omu 
                                        \v 10	nakumwamba ngwenyi, Vatu vosena veji
                                        \v 11	Echi alingile Yesu kuKana, limbo lya
                                        \v 12	Kufuma haze, ashikumukile kuKapelena
                                        \v 13	Chiwanyino chaKuzomboka chavaYuleya 
                                        \v 14	Kaha muTembele, awanyine vaze vapwil
                                        \v 15	Kaha anyotele chikote chajingoji nak
                                        Chapter 3
                                        \v 1	Jino kwapwile mutu wamuli vaFwaliseu
                                        \v 2	Ikiye ejile kuli Yesu naufuku nakwam
                                        \v 3	Yesu amukumbulwile ngwenyi, Mwamucha
                                        \v 4	Nyikotemu amuhulishile ngwenyi, Mutu
                                        \v 5	Yesu amukumbulwile ngwenyi, Mwamucha
                                        \v 6	Ocho chakusemuwa kumujimba wanyama c
                                        \v 7	Kanda ulikomokela omu ngwakwambanga 
                                        \v 8	Peho yeji kuhuhwanga kweshokwo nayis
                                        \v 9	Nyikotemu amuhulishile ngwenyi, Vyum
                                        \v 10	Yesu amukumbulwile ngwenyi, Ove umuk
                                        \v 11	Mwamuchano vene nangukulweza ngwami,
                                        \v 12	Omu ngunamilweze vyahamavu, kamwafwe
                                        \v 13	Naumwe aya lyehi mwilu wauchi, shina
                                        \v 14	Kaha ngana muze azezwile Mose uze li
                                        \v 15	mangana wose mwamufwelela, apwenga n
                                        ...
                                        ...
                                        ...
                                        Chapter 20
                                        \v 1	Jino halikumbi lyatete muchalumingo,
                                        \v 2	Shikaho alahile nakuya kuli vaShimon
                                        \v 3	Kaha Petulu alovokele nauze kambaji 
                                        \v 4	Vosena vavali vapwile nakulahila ham
                                        \v 5	Kaha hakunongama nakutalamo, amwene 
                                        \v 6	Kaha Shimona Petulu amukavile nakuhe
                                        \v 7	nalilesu lize lyapwile kumutwe wenyi
                                        \v 8	Haze vene nauze kambaji mukwavo áliv
                                        \v 9	Mwomwo haze, Mazu-vasoneka-ajila kan
                                        \v 10	Kaha vaze tumbaji vahilukile kuze va
                                        \v 11	Oloze Maliya emanyine haweluka wachi
                                        \v 12	Kaha amwene vangelo vavali vanavwale
                                        \v 13	Kaha vakiko vamuhulishile ngwavo, Ov
                                        \v 14	Omu áhanjikile ngocho, alumukile kah
                                        \v 15	Yesu amuhulishile ngwenyi, Ove pwevo
                                        Chapter 21
                                        \v 1	Kufuma haze, Yesu alisolwele cheka k
                                        \v 2	omu vapwile hamwe, Shimona Petulu na
                                        \v 3	Shimona Petulu avambile ngwenyi, Nan
                                        \v 4	Jino muze zwalala, Yesu emanyine kun
                                        \v 5	Kaha Yesu avahulishile ngwenyi, Enu 
                                        \v 6	Avambile ngwenyi, Fuchilenu kusali y
                                        \v 7	Kaha uze kambaji vazangile kuli Yesu
                                        \v 8	Vaze vakwavo tumbaji vejile muwato o
                                        \v 9	Kaha omu vakandukile hatunda, vamwen
                                        \v 10	Yesu avambile ngwenyi, Nehenuko vais
                                        \v 11	Shikaho Shimona Petulu engilile muwa
                                        \v 12	Yesu avambile ngwenyi, Twayenu mulya
                                        \v 13	Yesu ejile, anonele yize mbolo nakuv
                                        \v 14	Aka kakiko jino kamuchitatu Yesu ali
                                        \v 15	Omu vakumishile kulyaula, Yesu ahuli
                                        

                                        You’re probably wondering why it’s necessary to reverse the order of the lines, before applying the regex search/replacement ?

                                        Which, by the way, gives this temporary text :

                                        \v 15	Omu vakumishile kulyaula, Yesu ahuli
                                        \v 14	Aka kakiko jino kamuchitatu Yesu ali
                                        \v 13	Yesu ejile, anonele yize mbolo nakuv
                                        \v 12	Yesu avambile ngwenyi, Twayenu mulya
                                        \v 11	Shikaho Shimona Petulu engilile muwa
                                        \v 10	Yesu avambile ngwenyi, Nehenuko vais
                                        \v 9	Kaha omu vakandukile hatunda, vamwen
                                        \v 8	Vaze vakwavo tumbaji vejile muwato o
                                        \v 7	Kaha uze kambaji vazangile kuli Yesu
                                        \v 6	Avambile ngwenyi, Fuchilenu kusali y
                                        \v 5	Kaha Yesu avahulishile ngwenyi, Enu 
                                        \v 4	Jino muze zwalala, Yesu emanyine kun
                                        \v 3	Shimona Petulu avambile ngwenyi, Nan
                                        \v 2	omu vapwile hamwe, Shimona Petulu na
                                        \v 1	Kufuma haze, Yesu alisolwele cheka k
                                        Chapter 21
                                        \v 15	Yesu amuhulishile ngwenyi, Ove pwevo
                                        \v 14	Omu áhanjikile ngocho, alumukile kah
                                        \v 13	Kaha vakiko vamuhulishile ngwavo, Ov
                                        \v 12	Kaha amwene vangelo vavali vanavwale
                                        \v 11	Oloze Maliya emanyine haweluka wachi
                                        \v 10	Kaha vaze tumbaji vahilukile kuze va
                                        \v 9	Mwomwo haze, Mazu-vasoneka-ajila kan
                                        \v 8	Haze vene nauze kambaji mukwavo áliv
                                        \v 7	nalilesu lize lyapwile kumutwe wenyi
                                        \v 6	Kaha Shimona Petulu amukavile nakuhe
                                        \v 5	Kaha hakunongama nakutalamo, amwene 
                                        \v 4	Vosena vavali vapwile nakulahila ham
                                        \v 3	Kaha Petulu alovokele nauze kambaji 
                                        \v 2	Shikaho alahile nakuya kuli vaShimon
                                        \v 1	Jino halikumbi lyatete muchalumingo,
                                        Chapter 20
                                        ...
                                        ...
                                        ...
                                        \v 15	mangana wose mwamufwelela, apwenga n
                                        \v 14	Kaha ngana muze azezwile Mose uze li
                                        \v 13	Naumwe aya lyehi mwilu wauchi, shina
                                        \v 12	Omu ngunamilweze vyahamavu, kamwafwe
                                        \v 11	Mwamuchano vene nangukulweza ngwami,
                                        \v 10	Yesu amukumbulwile ngwenyi, Ove umuk
                                        \v 9	Nyikotemu amuhulishile ngwenyi, Vyum
                                        \v 8	Peho yeji kuhuhwanga kweshokwo nayis
                                        \v 7	Kanda ulikomokela omu ngwakwambanga 
                                        \v 6	Ocho chakusemuwa kumujimba wanyama c
                                        \v 5	Yesu amukumbulwile ngwenyi, Mwamucha
                                        \v 4	Nyikotemu amuhulishile ngwenyi, Mutu
                                        \v 3	Yesu amukumbulwile ngwenyi, Mwamucha
                                        \v 2	Ikiye ejile kuli Yesu naufuku nakwam
                                        \v 1	Jino kwapwile mutu wamuli vaFwaliseu
                                        Chapter 3
                                        \v 15	Kaha anyotele chikote chajingoji nak
                                        \v 14	Kaha muTembele, awanyine vaze vapwil
                                        \v 13	Chiwanyino chaKuzomboka chavaYuleya 
                                        \v 12	Kufuma haze, ashikumukile kuKapelena
                                        \v 11	Echi alingile Yesu kuKana, limbo lya
                                        \v 10	nakumwamba ngwenyi, Vatu vosena veji
                                        \v 9	Aze meya álumukile kupwa vinyo, omu 
                                        \v 8	Avambile ngwenyi, Switenumo jino mum
                                        \v 7	Yesu avambile ngwenyi, Zalisenu milo
                                        \v 6	Jino kuze kwapwile milondo yamalolwa
                                        \v 5	Naye alwezele vangamba ngwenyi, Ches
                                        \v 4	Kaha Yesu amwambile ngwenyi, Ove mam
                                        \v 3	Omu vinyo yahwile, naye alwezele Yes
                                        \v 2	Yesu nawa vamusanyikile natumbaji tw
                                        \v 1	Halikumbi lyamuchitatu kwapwile chil
                                        Chapter 2
                                        Chapter 1:14	Jino Lizu alilingishile Ivene ku
                                        Chapter 1:13	vaze vene vásemuwile, keshi kuma
                                        Chapter 1:12	Oloze veshovo vamutambwile, vaze
                                        Chapter 1:11	Ejile kuvyenyi, kaha vaze venyi 
                                        Chapter 1:10	Apwile hakaye, kaha kaye vakaten
                                        Chapter 1:9	Musana wamuchano apwile nakwiza 
                                        Chapter 1:8	Ikiye kapwile Uze Musanako, oloz
                                        Chapter 1:7	Ikiye ejile apwenga chinjiho wak
                                        Chapter 1:6	Kwapwile mutu vatumine kufuma ku
                                        Chapter 1:5	Musana kana wamunyika mumilima, 
                                        Chapter 1:4	Muli Ikiye mwapwile Mwono, kaha 
                                        Chapter 1:3	Vyosena vavitengelele muli Ikiye
                                        Chapter 1:2	Ikiye apwilenga naKalunga kukupu
                                        Chapter 1:1	Kukuputuka Lizu apwilengako, kah
                                        Chapter 1
                                        

                                        Well, the reason is that regular epressions can possibly take into account text placed AFTER the current position, even after several lines, but can generally never take into account text placed BEFORE the current position !


                                        So, let’s examine, first, the regex (?s-i)\\v .+^Chapter \d+$, against the text, AFTER the reverse operation :

                                        Starting with the caret at the beginning of the line \v 15 Omu vakumishile kulyaula, Yesu ahuli, this regex would grab all the text till the string Chapter 1 . This happens because the initial modifiers (?s-i) mean that, both :

                                        • Any character, even the line-break chars, are considered, due to the (?s) syntax : s = single line

                                        • Any character must respect the case, due to the (?-i) syntax : -i = non insensitive

                                        Of course, this regex is useless as the line \v 15 Omu vakumishile kulyaula, Yesu ahuli must refer to Chapter 21 and not to Chapter 1


                                        Now, in order to only consider the text till the nearest string Chapter, let’s try this regex (?s-i)\\v .+?^Chapter \d+$. Ah…, the result is better because it just selects the fifteen lines ( from \v15 to \v1 and the Chapter 21 string

                                        First, it detects the \v string ( the double antislash is needed as the antislash is itself a regex character ), followed with a space char and, then, any char ( . ) , included possible line-breaks, repeated ( + ) till the nearest string ( ? ) Chapter beginning a line ( ^ ) and followed with a space and a non-empty list of digits ( \d+ ) ending the current line ( $ )

                                        As we presently need to always know the Chapter ## string, we must store the part ^Chapter \d+$ with parentheses. So, the regex becomes (?s-i)\\v .+?^(Chapter \d+$)


                                        However, that’s not enough because the string Chapter 21 must be used for each line from \v15 to \v1. Thus, we’ll use a new structure, called a look-ahead and the regex becomes (?s-i)\\v (?=.+?^(Chapter \d+$)). If you run this new regex against the text, it should only select the \v string followed with a space char but, it is implied, on condition that the nearest stored Chapter ## string is found further on !

                                        And, in that case, it replaces the \v15 string and its following space char, by the stored string ( $1 = Chapter 21) followed with a colon char, giving the line Chapter 21:15 Omu vakumishile kulyaula, Yesu ahuli

                                        Then it would select the \v string and the space before number 14 and would replace it by the line Chapter 21:14 Omu vakumishile kulyaula, Yesu ahuli and so on till the line Chapter 21:1 Kufuma haze, Yesu alisolwele cheka k

                                        Now, the next match is \v and the space, part of the Chapter 20 and, indeed, after the replacement we get the line : Chapter 20:15 Yesu amuhulishile ngwenyi, Ove pwevo and so on…


                                        Finally, as you said that the “Chapter” is actually a book name, we just have to change the literal name Chapter by the regex ^(\u[\u\l -]+ which means :

                                        • Find an uppercase letter, beginning the line ( ^\u )

                                        • followed by a choice of an upper or a lower case letter or a space char or a dash char ( [\u\l -] ), repeated as many times as necessary ( [\u\l -]+ )


                                        To end, with an other Edit > Line Operations > Reverse Line order action, we simply get the natural order of your text !

                                        If you’re still unsure about certain details, just ask me !

                                        Best Regards,

                                        guy038

                                        Robert Or Janet DiebelR 3 Replies Last reply Reply Quote 0
                                        • Robert Or Janet DiebelR
                                          Robert Or Janet Diebel @guy038
                                          last edited by

                                          @guy038 said in RegEx Help with Backreference I think:

                                          (?s-i)\v (?=.+?^(\u[\u\l -]+ \d+$))

                                          Sorry, but I am getting invalid RegEX with this new one.

                                          Alan KilbornA 1 Reply Last reply Reply Quote 0
                                          • Robert Or Janet DiebelR
                                            Robert Or Janet Diebel @guy038
                                            last edited by

                                            @guy038 I am investigating why what was working is giving errors.
                                            Your explanation is excellent and helpful.
                                            I will get back to you ASAP.
                                            I do not want to waste your time.

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