Page 1 of 2

Regexp Expression

Posted: Fri Dec 01, 2023 2:22 pm
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

Re: Regexp Expression

Posted: Fri Dec 01, 2023 2:27 pm
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

Re: Regexp Expression

Posted: Fri Dec 01, 2023 2:27 pm
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.

Re: Regexp Expression

Posted: Fri Dec 01, 2023 2:46 pm
by loulou2522
Thanks Jacdelad
I try on my computer and apparently variable are empty and regexp is not good. Have you an idea ?

Re: Regexp Expression

Posted: Fri Dec 01, 2023 2:48 pm
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.

Re: Regexp Expression

Posted: Fri Dec 01, 2023 2:55 pm
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.

Re: Regexp Expression

Posted: Fri Dec 01, 2023 3:01 pm
by loulou2522
I maje an error on the text
the right text is
myInput$="AS 18 000 AE 25 000 35 000"

Re: Regexp Expression

Posted: Fri Dec 01, 2023 3:08 pm
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

Re: Regexp Expression

Posted: Fri Dec 01, 2023 3:11 pm
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

Re: Regexp Expression

Posted: Fri Dec 01, 2023 3:13 pm
by loulou2522
Thanks jackdelad that's work goof know

Re: Regexp Expression

Posted: Sat Dec 02, 2023 1:06 pm
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

Re: Regexp Expression

Posted: Sat Dec 02, 2023 4:41 pm
by jacdelad
Add a + after each space in the regexp or use

Code: Select all

While FindString(MyString$," ")
  MyString$=ReplaceString(MyString$," ","")
Wend
before evaluating.

Re: Regexp Expression

Posted: Sat Dec 02, 2023 5:18 pm
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

Re: Regexp Expression

Posted: Sat Dec 02, 2023 6:04 pm
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

Re: Regexp Expression

Posted: Sat Dec 02, 2023 6:08 pm
by loulou2522
this sentence is
if CreateRegularExpression(1,"\d+\K[\h]+(?=\d{3})" , #PB_RegularExpression_NoCase)