RegEx Help with Backreference I think
-
@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
-
@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:
- Use Reverse Line Order which is under Edit menu, then Line Operations.
- 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 - Use Reverse Line Order which is under Edit menu, then Line Operations.
Terry
-
@Terry-R
Thanx very much! -
@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? -
@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
-
@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. -
@Robert-Or-Janet-Diebel
Macro worked great.
Thanx again! -
@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.
-
@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 theChapter<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.
-
@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
-
@Terry-R It is the first word, but is repeated for each Chapter
-
@Robert-Or-Janet-Diebel
Thanx!
That appears to have worked.
Wishing you a great day. -
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 theChapter
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
-
-
@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-Diebel
Adds Book name fine, but all chapters show as “1” -
@Robert-Or-Janet-Diebel
Could be my fault. I am still testing -
@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
-
@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? -
@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. -
@Terry-R I am trying to put together an example.
Will post soon I hope.