RegEx help needed
RegEx help needed
Hi,
I am trying to find each ong of this list
texto.s = "\n\n1. Let It Be\n2. American Woman\n3. War\n4. Ain't No Mountain High Enough\n5. The Long and Winding Road\n6. Band of Gold\n7. My Sweet Lord\n8. Candida\n9. Make It With You\n10. The Love You Save\n11. Cracklin' Rosie\n12. Reflections of My Life"
But i tried many regex and even ask chatgpt for help, but everything return me no matches found.
I dont want to get the index for each song, just the song name.
Can any body help me please?
Thanks in advance
I am trying to find each ong of this list
texto.s = "\n\n1. Let It Be\n2. American Woman\n3. War\n4. Ain't No Mountain High Enough\n5. The Long and Winding Road\n6. Band of Gold\n7. My Sweet Lord\n8. Candida\n9. Make It With You\n10. The Love You Save\n11. Cracklin' Rosie\n12. Reflections of My Life"
But i tried many regex and even ask chatgpt for help, but everything return me no matches found.
I dont want to get the index for each song, just the song name.
Can any body help me please?
Thanks in advance
ARGENTINA WORLD CHAMPION
Re: RegEx help needed
RegEx is not needed for this simple text if it always uses this exact format. Just use StringField to separate them all:
The output:
Code: Select all
texto.s="\n\n1. Let It Be\n2. American Woman\n3. War\n4. Ain't No Mountain High Enough\n5. The Long and Winding Road\n6. Band of Gold\n7. My Sweet Lord\n8. Candida\n9. Make It With You\n10. The Love You Save\n11. Cracklin' Rosie\n12. Reflections of My Life"
c=CountString(texto,"\n")+1
For i=1 To c
song$=StringField(texto,i,"\n")
If song$<>""
Debug Trim(Mid(song$,FindString(song$,". ")+1))
EndIf
Next
Code: Select all
Let It Be
American Woman
War
Ain't No Mountain High Enough
The Long and Winding Road
Band of Gold
My Sweet Lord
Candida
Make It With You
The Love You Save
Cracklin' Rosie
Reflections of My Life
Re: RegEx help needed
(Deleted: with my regex, the last sentence is missing (\n12. Reflections of My Life))
This works (capture also last #12)
But not in PB (even with ~ )

This works (capture also last #12)
Code: Select all
n\d+\. (.+?[\\"])

Code: Select all
~"n\\d+\\. (.+?[\\\"])"

Last edited by Marc56us on Sat Mar 18, 2023 5:26 pm, edited 6 times in total.
Re: RegEx help needed
Why not just use an array?
Code: Select all
Procedure.i SplitString(String.s, Delimiter.s, Array Output.s(1)) ; by kenmo
n = 1 + CountString(String, Delimiter)
Dim Output.s(n - 1)
For i = 0 To n-1
Output(i) = Trim(StringField(String, 1+i, Delimiter))
Next i
EndProcedure
Dim Word.s(0)
mytext.s="Let It Be,American Woman,War,Ain't No Mountain High Enough,The Long and Winding Road,Band of Gold,My Sweet Lord,Candida,Make It With You,The Love You Save,Cracklin' Rosie,Reflections of My Life"
SplitString(myText, ",", Word())
For i = 0 To ArraySize(Word())
Debug Word(i)
Next i
- It was too lonely at the top.
System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
Re: RegEx help needed
Code: Select all
EnableExplicit
Define texto.s, rex, Count, I
Define Dim Word.s(0)
texto = "\n\n1. Let It Be\n2. American Woman\n3. War\n4. Ain't No Mountain High Enough\n5. The Long and Winding Road\n6. Band of Gold\n7. My Sweet Lord\n8. Candida\n9. Make It With You\n10. The Love You Save\n11. Cracklin' Rosie\n12. Reflections of My Life"
rex = CreateRegularExpression(#PB_Any, "\d+\. \K.+?(?=\\n)", #PB_RegularExpression_NoCase)
If rex
Count = ExtractRegularExpression(rex, texto, Word())
FreeRegularExpression(rex)
EndIf
; For i = 0 To ArraySize(Word())
For i = 0 To Count - 1
Debug Word(i)
Next
Re: RegEx help needed
@BlueB: Your example ignores "\n" and the number and dot at the start of each song name. That's what the original text has. Also, many song titles have a comma in them ("ABBA - I Do, I Do, I Do, I Do, I Do"), so a comma can't be used as the delimiter.
Last edited by BarryG on Sun Mar 19, 2023 8:47 am, edited 1 time in total.
Re: RegEx help needed
Wouldn't adding \Z work right?
"Let It Be
American Woman
War
Ain't No Mountain High Enough
The Long and Winding Road
Band of Gold
My Sweet Lord
Candida
Make It With You
The Love You Save
Cracklin' Rosie
Reflections of My Life"
Code: Select all
rex = CreateRegularExpression(#PB_Any, "\d+\. \K.+?(?=\\n|\Z)", #PB_RegularExpression_NoCase)
American Woman
War
Ain't No Mountain High Enough
The Long and Winding Road
Band of Gold
My Sweet Lord
Candida
Make It With You
The Love You Save
Cracklin' Rosie
Reflections of My Life"
Marc56us wrote: Sat Mar 18, 2023 9:24 am (Deleted: with my regex, the last sentence is missing (\n12. Reflections of My Life))
This works (capture also last #12)But not in PB (even with ~ )Code: Select all
n\d+\. (.+?[\\"])
![]()
Code: Select all
~"n\\d+\\. (.+?[\\\"])"
![]()
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Re: RegEx help needed
Hi,Thunder93 wrote: Sun Mar 19, 2023 6:02 am Wouldn't adding \Z work right?
Code: Select all
rex = CreateRegularExpression(#PB_Any, "\d+\. \K.+?(?=\\n|\Z)", #PB_RegularExpression_NoCase)
Can you put a complete (and tested) code example ? because in the example where this RegEx comes from the last element (n12) is not extracted.
Code: Select all
EnableExplicit
Define texto.s, rex, Count, I
Define Dim Word.s(0)
texto = "\n\n1. Let It Be\n2. American Woman\n3. War\n4. Ain't No Mountain High Enough\n5. The Long and Winding Road\n6. Band of Gold\n7. My Sweet Lord\n8. Candida\n9. Make It With You\n10. The Love You Save\n11. Cracklin' Rosie\n12. Reflections of My Life"
rex = CreateRegularExpression(#PB_Any, "\d+\. \K.+?(?=\\n)", #PB_RegularExpression_NoCase)
If rex
Count = ExtractRegularExpression(rex, texto, Word())
FreeRegularExpression(rex)
EndIf
; For i = 0 To ArraySize(Word())
For i = 0 To Count - 1
Debug Word(i)
Next
Code: Select all
Let It Be
American Woman
War
Ain't No Mountain High Enough
The Long and Winding Road
Band of Gold
My Sweet Lord
Candida
Make It With You
The Love You Save
Cracklin' Rosie
Both RegEx (Azjio's and mine) work well in another program (e.g. Notepad++) but not in PB.
Another problem of interpretation between PB and PCRE
Tested on 6.01 LTS x64 Windows

Re: RegEx help needed
Hi Marc56us, I was using AZJIO's code with the tweak on the line as shown in my previous post. Works perfect for me, using 6.01 LTS Windows x64 & x86.


Last edited by Thunder93 on Sun Mar 19, 2023 10:26 am, edited 1 time in total.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Re: RegEx help needed
Right, typo ^c/^v, sorry , thank you two 
Edit: I realized my mistake: I was trying to enclose the last term with the ( " ) quotation mark ... which was part of the sample code and not of the string itself.
Version using groups

Edit: I realized my mistake: I was trying to enclose the last term with the ( " ) quotation mark ... which was part of the sample code and not of the string itself.
Version using groups
Code: Select all
EnableExplicit
Define Txt$ = "\n\n1. Let It Be\n2. American Woman\n3. War\" +
"n4. Ain't No Mountain High Enough\n5. The Long and Winding Road\" +
"n6. Band of Gold\n7. My Sweet Lord\n8. Candida\n9. Make It With You\" +
"n10. The Love You Save\n11. Cracklin' Rosie\n12. Reflections of My Life"
If Not CreateRegularExpression(0, "\d+\. (.+?)(?:\\|\z)")
Debug RegularExpressionError() : End
EndIf
If ExamineRegularExpression(0, Txt$)
While NextRegularExpressionMatch(0)
Debug RegularExpressionGroup(0, 1)
Wend
FreeRegularExpression(0)
EndIf
End
Code: Select all
Let It Be
American Woman
War
Ain't No Mountain High Enough
The Long and Winding Road
Band of Gold
My Sweet Lord
Candida
Make It With You
The Love You Save
Cracklin' Rosie
Reflections of My Life