RegEx help needed

Just starting out? Need help? Post your questions and find answers here.
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

RegEx help needed

Post 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
ARGENTINA WORLD CHAMPION
BarryG
Addict
Addict
Posts: 4178
Joined: Thu Apr 18, 2019 8:17 am

Re: RegEx help needed

Post 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
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: RegEx help needed

Post by Marc56us »

(Deleted: with my regex, the last sentence is missing (\n12. Reflections of My Life))
This works (capture also last #12)

Code: Select all

n\d+\. (.+?[\\"])
But not in PB (even with ~ ) :?:

Code: Select all

~"n\\d+\\. (.+?[\\\"])"
:x
Last edited by Marc56us on Sat Mar 18, 2023 5:26 pm, edited 6 times in total.
User avatar
blueb
Addict
Addict
Posts: 1116
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: RegEx help needed

Post 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
- 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
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

Re: RegEx help needed

Post 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
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: RegEx help needed

Post by ricardo »

Thanks for all your answers and help !!
ARGENTINA WORLD CHAMPION
BarryG
Addict
Addict
Posts: 4178
Joined: Thu Apr 18, 2019 8:17 am

Re: RegEx help needed

Post 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.
Last edited by BarryG on Sun Mar 19, 2023 8:47 am, edited 1 time in total.
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: RegEx help needed

Post 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)

Code: Select all

n\d+\. (.+?[\\"])
But not in PB (even with ~ ) :?:

Code: Select all

~"n\\d+\\. (.+?[\\\"])"
:x
ʽʽ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
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: RegEx help needed

Post 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
:wink:
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: RegEx help needed

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

Image
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
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

Re: RegEx help needed

Post by AZJIO »

\z
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: RegEx help needed

Post by Marc56us »

Right, typo ^c/^v, sorry , thank you two 8)

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
Post Reply