Regexp Expression

Just starting out? Need help? Post your questions and find answers here.
loulou2522
Enthusiast
Enthusiast
Posts: 553
Joined: Tue Oct 14, 2014 12:09 pm

Regexp Expression

Post by loulou2522 »

I want to extract in thos text
AD 10 000 AF 20;000 30 000
the fowlowing text in five variable
AD / 10000/ AF / 20000 / 30000
with regexp but i don't arrive.
Can someone help Me ?
Thanks in davance
User avatar
jacdelad
Addict
Addict
Posts: 2035
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Regexp Expression

Post by jacdelad »

Assuming you always use this exact pattern:

Code: Select all

myInput$="10 000 20;000 30 000"

CreateRegularExpression(0,"(\d+ \d+) (\d+;\d+) (\d+ \d+)")
If ExamineRegularExpression(0,myInput$) And NextRegularExpressionMatch(0)
  var1.l=Val(ReplaceString(RegularExpressionGroup(0,1)," ",""))
  var2.l=Val(ReplaceString(RegularExpressionGroup(0,2),";",""))
  var3.l=Val(ReplaceString(RegularExpressionGroup(0,3)," ",""))
EndIf

Debug var1
Debug var2
Debug var3
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
jacdelad
Addict
Addict
Posts: 2035
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Regexp Expression

Post by jacdelad »

Oh come on, you changed your post while I was solving your problem...

Code: Select all

myInput$="AD 10 000 AF 20;000 30 000"

CreateRegularExpression(0,"(.+) (\d+ \d+) (.+) (\d+;\d+) (\d+ \d+)")
If ExamineRegularExpression(0,myInput$) And NextRegularExpressionMatch(0)
  var1.s=RegularExpressionGroup(0,1)
  var2.l=Val(ReplaceString(RegularExpressionGroup(0,2)," ",""))
  var3.s=RegularExpressionGroup(0,3)
  var4.l=Val(ReplaceString(RegularExpressionGroup(0,4),";",""))
  var5.l=Val(ReplaceString(RegularExpressionGroup(0,5)," ",""))
EndIf

Debug var1
Debug var2
Debug var3
Debug var4
Debug var5
But only, if it your data follows your exact definition.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
loulou2522
Enthusiast
Enthusiast
Posts: 553
Joined: Tue Oct 14, 2014 12:09 pm

Re: Regexp Expression

Post by loulou2522 »

Thanks Jacdelad
I try on my computer and apparently variable are empty and regexp is not good. Have you an idea ?
User avatar
NicTheQuick
Addict
Addict
Posts: 1529
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Regexp Expression

Post by NicTheQuick »

It's working on my side.
If it does not work on your side, you changed something and you should tell us about that.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
jacdelad
Addict
Addict
Posts: 2035
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Regexp Expression

Post by jacdelad »

loulou2522 wrote: Fri Dec 01, 2023 2:46 pm Thanks Jacdelad
I try on my computer and apparently variable are empty and regexp is not good. Have you an idea ?
Like Nic wrote, it should work. RegExp can be an excellent option, if your input always follows the input you gave us. If not...there might be other options shich suit you better. We can help better with more information.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
loulou2522
Enthusiast
Enthusiast
Posts: 553
Joined: Tue Oct 14, 2014 12:09 pm

Re: Regexp Expression

Post by loulou2522 »

I maje an error on the text
the right text is
myInput$="AS 18 000 AE 25 000 35 000"
AZJIO
Addict
Addict
Posts: 2227
Joined: Sun May 14, 2017 1:48 am

Re: Regexp Expression

Post by AZJIO »

Code: Select all

Define myInput$="AD 10 000 AF 20;000 30 000"
Define Dim Result$(0)
Define i, NbFound

If CreateRegularExpression(1," \d+\K[\h;](?=\d{3})", #PB_RegularExpression_NoCase)
	myInput$ = ReplaceRegularExpression(1, myInput$, "")
Else
    Debug RegularExpressionError()
EndIf

; Debug myInput$

If CreateRegularExpression(0,"[a-z\d]+", #PB_RegularExpression_NoCase)
    Dim Result$(0)
    NbFound = ExtractRegularExpression(0, myInput$, Result$())
    For i = 0 To NbFound-1
        Debug Result$(i)
    Next
Else
    Debug RegularExpressionError()
EndIf
Last edited by AZJIO on Fri Dec 01, 2023 3:13 pm, edited 1 time in total.
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Regexp Expression

Post by Marc56us »

loulou2522 wrote: Fri Dec 01, 2023 3:01 pm I maje an error on the text
the right text is
myInput$="AS 18 000 AE 25 000 35 000"
For those who don't like RegEx :mrgreen:

Code: Select all

myInput$="AS 18 000 AE 25 000 35 000"

A$ = StringField(myInput$, 1, " ")
B$ = StringField(myInput$, 2, " ")
C$ = StringField(myInput$, 3, " ")
D$ = StringField(myInput$, 4, " ")
E$ = StringField(myInput$, 5, " ")
F$ = StringField(myInput$, 6, " ")
G$ = StringField(myInput$, 7, " ")
H$ = StringField(myInput$, 8, " ")

Debug A$
Debug B$ + C$
Debug D$
Debug E$ + F$
Debug G$ + H$

; AS
; 18000
; AE
; 25000
; 35000
loulou2522
Enthusiast
Enthusiast
Posts: 553
Joined: Tue Oct 14, 2014 12:09 pm

Re: Regexp Expression

Post by loulou2522 »

Thanks jackdelad that's work goof know
loulou2522
Enthusiast
Enthusiast
Posts: 553
Joined: Tue Oct 14, 2014 12:09 pm

Re: Regexp Expression

Post by loulou2522 »

Now i have another problem if in the variable there is more thant 1 space betwwen 5 000 like 5 000 (two or more space) these expression doen't match

Code: Select all

var11= "AF 35 000 AD 45   000   56 000"

f CreateRegularExpression(1," \d+\K[\h;.](?=\d{3})", #PB_RegularExpression_NoCase)
  var11 = ReplaceRegularExpression(1, var11, "")
Else
  Debug RegularExpressionError()
EndIf
If CreateRegularExpression(0,"[a-f\d]+", #PB_RegularExpression_NoCase)
  Dim Result$(0)
  NbFound = ExtractRegularExpression(0, var11, Result$())
  For i = 0 To NbFound-1
    Debug Result$(i)
  Next
  FreeRegularExpression(0)
EndIf 
Any help would be appreciated?
Thanks
User avatar
jacdelad
Addict
Addict
Posts: 2035
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Regexp Expression

Post by jacdelad »

Add a + after each space in the regexp or use

Code: Select all

While FindString(MyString$," ")
  MyString$=ReplaceString(MyString$," ","")
Wend
before evaluating.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
loulou2522
Enthusiast
Enthusiast
Posts: 553
Joined: Tue Oct 14, 2014 12:09 pm

Re: Regexp Expression

Post by loulou2522 »

Jackdelad,
Sorry but i don't arrive to make what you indicate me. Can you write the right sentence for regexp. I don't know the meaning of \K and \h
AZJIO
Addict
Addict
Posts: 2227
Joined: Sun May 14, 2017 1:48 am

Re: Regexp Expression

Post by AZJIO »

loulou2522 wrote: Sat Dec 02, 2023 5:18 pm if in the variable there is more thant 1 space
Old
" \d+\K[\h;.](?=\d{3})"
New
" \d+\K[\h;.]+(?=\d{3})"
loulou2522 wrote: Sat Dec 02, 2023 5:18 pm Can you write the right sentence for regexp. I don't know the meaning of \K and \h
\h = [ \t] - Any horizontal space, tabulation - Chr (9), Chr (32), Chr (160)
\K - to the left of \K the previous coincidence. Is it similar (?<=...), but at the same time does not have a restriction of the exact length
My instructions in Russian, use Google translator
Last edited by AZJIO on Sat Dec 02, 2023 6:10 pm, edited 1 time in total.
loulou2522
Enthusiast
Enthusiast
Posts: 553
Joined: Tue Oct 14, 2014 12:09 pm

Re: Regexp Expression

Post by loulou2522 »

this sentence is
if CreateRegularExpression(1,"\d+\K[\h]+(?=\d{3})" , #PB_RegularExpression_NoCase)
Post Reply