Page 1 of 1
RegEx help needed
Posted: Sat Mar 18, 2023 12:26 am
by ricardo
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
Re: RegEx help needed
Posted: Sat Mar 18, 2023 1:52 am
by BarryG
RegEx is not needed for this simple text if it always uses this exact format. Just use StringField to separate them all:
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
The output:
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
Posted: Sat Mar 18, 2023 9:24 am
by Marc56us
(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 ~ )

Re: RegEx help needed
Posted: Sat Mar 18, 2023 1:14 pm
by blueb
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
Re: RegEx help needed
Posted: Sat Mar 18, 2023 1:53 pm
by AZJIO
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
Posted: Sat Mar 18, 2023 4:02 pm
by ricardo
Thanks for all your answers and help !!
Re: RegEx help needed
Posted: Sun Mar 19, 2023 2:13 am
by BarryG
@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.
Re: RegEx help needed
Posted: Sun Mar 19, 2023 6:02 am
by Thunder93
Wouldn't adding \Z work right?
Code: Select all
rex = CreateRegularExpression(#PB_Any, "\d+\. \K.+?(?=\\n|\Z)", #PB_RegularExpression_NoCase)
"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"
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 ~ )
Re: RegEx help needed
Posted: Sun Mar 19, 2023 9:52 am
by Marc56us
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)
Hi,
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
Last item not captured « \n12. Reflections of My Life »
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
Posted: Sun Mar 19, 2023 10:21 am
by Thunder93
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.

Re: RegEx help needed
Posted: Sun Mar 19, 2023 10:23 am
by AZJIO
\z
Re: RegEx help needed
Posted: Sun Mar 19, 2023 10:53 am
by Marc56us
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
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