• Login
Community
  • Login

RegEx Help with Backreference I think

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
58 Posts 4 Posters 2.0k 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.
  • R
    Robert Or Janet Diebel @guy038
    last edited by Mar 18, 2025, 6:21 PM

    @guy038
    Getting a colon before Chapters.
    I can remove by another RegEx.
    But if you can help me fix it in your code, that would be great.

    Chapter 3:15	mangana wose mwamufwelela, apwenga n
    :Chapter 4
    
    1 Reply Last reply Reply Quote 0
    • T
      Terry R @Robert Or Janet Diebel
      last edited by Mar 18, 2025, 6:27 PM

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

      I think a RegEx could do this, but I need help.

      This question is almost an exact match for the post #26693 in that it looks like it needs a lookbehind. Of course lookbehinds cannot be variable length so it can’t be achieved.

      As in that post, my solution is to reverse the lines, then the lookbehind becomes a lookahead and can easily be achieved. Lastly the lines are again reversed. These 3 steps can all be recorded as a macro if this is a repeated process.

      So the steps are:

      1. Use Reverse Line Order which is under Edit menu, then Line Operations.
      2. Using the Replace function we have (Have the cursor in the first line of the file):
        Find What:(?-s)^\\v (?=(.+\R)+?(Chapter \d+))
        Replace With:${2}:
        Select Replace All
      3. Use Reverse Line Order which is under Edit menu, then Line Operations.

      Terry

      R 2 Replies Last reply Mar 18, 2025, 6:40 PM Reply Quote 2
      • R
        Robert Or Janet Diebel @Terry R
        last edited by Mar 18, 2025, 6:40 PM

        @Terry-R
        Thanx very much!

        1 Reply Last reply Reply Quote 0
        • R
          Robert Or Janet Diebel @Terry R
          last edited by Mar 18, 2025, 6:54 PM

          @Terry-R
          That is amazing!
          How do I “reverse line order” in a macro?
          Or do I need to have more than one macro, and reverse in between manually?

          T 1 Reply Last reply Mar 18, 2025, 7:00 PM Reply Quote 0
          • T
            Terry R @Robert Or Janet Diebel
            last edited by Mar 18, 2025, 7:00 PM

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

            How do I “reverse line order” in a macro?

            In Notepad++ a macro is essentially all the keystrokes (and mouse selections) together with keyboard input recorded. Once saved (and a shortcut can be assigned to it as well), it can be played back. The playing back just replays those keystrokes and mouse selections etc. So yes the reverse line order can also be recorded.

            The online manual has the detail and can explain it much better than I can. You find the link from the ? menu.

            Terry

            R 1 Reply Last reply Mar 18, 2025, 7:06 PM Reply Quote 1
            • R
              Robert Or Janet Diebel @Terry R
              last edited by Mar 18, 2025, 7:06 PM

              @Terry-R
              As I am changing just under 1300 chapters with a lot more than 15 verses, this will save me many hours.
              Your help is much appreciated!
              I will indeed build a macro.

              R 2 Replies Last reply Mar 18, 2025, 7:39 PM Reply Quote 1
              • R
                Robert Or Janet Diebel @Robert Or Janet Diebel
                last edited by Mar 18, 2025, 7:39 PM

                @Robert-Or-Janet-Diebel
                Macro worked great.
                Thanx again!

                1 Reply Last reply Reply Quote 0
                • R
                  Robert Or Janet Diebel @Robert Or Janet Diebel
                  last edited by Mar 18, 2025, 8:41 PM

                  @Robert-Or-Janet-Diebel
                  One more thing please.
                  The “Chapter” is actually a book name.
                  It is the first word in the file. I need that to be read into a variable and put in the beginning of the line instead of the word “Chapter”

                  I am working on it, but if you can help that would be appreciated.

                  T 1 Reply Last reply Mar 18, 2025, 8:57 PM Reply Quote 0
                  • T
                    Terry R @Robert Or Janet Diebel
                    last edited by Terry R Mar 18, 2025, 9:02 PM Mar 18, 2025, 8:57 PM

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

                    It is the first word in the file. I need that to be read into a variable and put in the beginning of the line instead of the word “Chapter”

                    Well, obviously some examples would be extremely helpful, otherwise I’m just guessing.

                    And my first guess would be to have this as the regex:
                    (?-s)^\\v (?=(.+\R)+?(.+?\d+$))
                    You will see I’ve changed the Chapter<space>\d+ to .+?\d+$. The .+? means to consume characters in a non-greedy fashion and stop when a digit appears. Then the rest says to also consume digits (as many as occurs together) and stop at the end of the line ($). So if there are spaces behind the digits it won’t work. Without examples I can’t be sure.

                    Terry

                    PS you said first word in the file. So do you really mean it’s the word at the start of each group OR truly ONLY the first line in the file. If ONLY the first line then that is an entirely different sort of regex. Examples with say 2-4 groups of verses. And AGAIN include 2 code boxes, 1st being existing data and 2nd being how you want it to look.

                    R 2 Replies Last reply Mar 18, 2025, 9:03 PM Reply Quote 0
                    • R
                      Robert Or Janet Diebel @Terry R
                      last edited by Robert Or Janet Diebel Mar 18, 2025, 9:04 PM Mar 18, 2025, 9:03 PM

                      @Terry-R Thanx.
                      I need the first word in this file to be used for all “\v”
                      The next file will be a different book name and so needs to read the first word, and use it.

                      Samu 1
                      \v 1:1	Kukuputuka Lizu apwilengako, kah
                      \v 1:2	Ikiye apwilenga naKalunga kukupu
                      \v 1:3	Vyosena vavitengelele muli Ikiye
                      \v 1:4	Muli Ikiye mwapwile Mwono, kaha 
                      \v 1:5	Musana kana wamunyika mumilima, 
                      \v 1:6	Kwapwile mutu vatumine kufuma ku
                      \v 1:7	Ikiye ejile apwenga chinjiho wak
                      \v 1:8	Ikiye kapwile Uze Musanako, oloz
                      \v 1:9	Musana wamuchano apwile nakwiza 
                      \v 1:10	Apwile hakaye, kaha kaye vakaten
                      \v 1:11	Ejile kuvyenyi, kaha vaze venyi 
                      \v 1:12	Oloze veshovo vamutambwile, vaze
                      \v 1:13	vaze vene vásemuwile, keshi kuma
                      \v 1:14	Jino Lizu alilingishile Ivene ku
                      Samu 2
                      \v 1	Halikumbi lyamuchitatu kwapwile chil
                      \v 2	Yesu nawa vamusanyikile natumbaji tw
                      \v 3	Omu vinyo yahwile, naye alwezele Yes
                      
                      1 Reply Last reply Reply Quote 0
                      • R
                        Robert Or Janet Diebel @Terry R
                        last edited by Mar 18, 2025, 9:07 PM

                        @Terry-R It is the first word, but is repeated for each Chapter

                        R 1 Reply Last reply Mar 18, 2025, 9:11 PM Reply Quote 0
                        • R
                          Robert Or Janet Diebel @Robert Or Janet Diebel
                          last edited by Mar 18, 2025, 9:11 PM

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

                          1 Reply Last reply Reply Quote 0
                          • G
                            guy038
                            last edited by guy038 Mar 18, 2025, 11:12 PM Mar 18, 2025, 11:06 PM

                            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

                            R 2 Replies Last reply Mar 19, 2025, 3:11 PM Reply Quote 1
                            • R
                              Robert Or Janet Diebel @guy038
                              last edited by Mar 19, 2025, 3:11 PM

                              @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.

                              R 1 Reply Last reply Mar 19, 2025, 3:24 PM Reply Quote 0
                              • R
                                Robert Or Janet Diebel @Robert Or Janet Diebel
                                last edited by Mar 19, 2025, 3:24 PM

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

                                R 1 Reply Last reply Mar 19, 2025, 3:35 PM Reply Quote 0
                                • R
                                  Robert Or Janet Diebel @Robert Or Janet Diebel
                                  last edited by Mar 19, 2025, 3:35 PM

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

                                  T 1 Reply Last reply Mar 19, 2025, 5:17 PM Reply Quote 0
                                  • T
                                    Terry R @Robert Or Janet Diebel
                                    last edited by Mar 19, 2025, 5:17 PM

                                    @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

                                    R 3 Replies Last reply Mar 19, 2025, 5:52 PM Reply Quote 0
                                    • R
                                      Robert Or Janet Diebel @Terry R
                                      last edited by Mar 19, 2025, 5:52 PM

                                      @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
                                      • R
                                        Robert Or Janet Diebel @guy038
                                        last edited by Robert Or Janet Diebel Mar 19, 2025, 6:05 PM Mar 19, 2025, 6:04 PM

                                        @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
                                        • R
                                          Robert Or Janet Diebel @Terry R
                                          last edited by Mar 19, 2025, 6:20 PM

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

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