Page 1 of 1

Help with regexp

Posted: Wed Aug 14, 2024 7:34 am
by loulou2522
Here is my text
. Emprunts 319 811 | 27,79 51 478 | 5.84
I want to capture on the same row 319 811 51 478
GROUP 1 319 811" group2 51 478
OR
. Emprunts 51 478 | 5.84
GROUP 1 "" group2 51478
Can someo,ne help me ?

Re: Help with regexp

Posted: Wed Aug 14, 2024 8:03 am
by Marc56us
Here is my text
. Emprunts 319 811 | 27,79 51 478 | 5.84
I want to capture on the same row 319 811 51 478
GROUP 1 319 811" group2 51 478
OR
. Emprunts 51 478 | 5.84
GROUP 1 "" group2 51478
Can someo,ne help me ?
I assume that the numbers given here use space as the thousands separator?
So this can go for the first case.

Code: Select all

EnableExplicit

If Not CreateRegularExpression(0, "(\d[\d ]+\d).+? (\d[\d ]+\d)")
    Debug RegularExpressionError() : End
EndIf

Define Txt$ = ". Emprunts 319 811 | 27,79 51 478 | 5.84"

If Not ExamineRegularExpression(0, Txt$)
    Debug "Nothing" : End
EndIf

Define i
While NextRegularExpressionMatch(0)
    i + 1
    Debug "#1 [" + RegularExpressionGroup(0, 1) + "]"
    Debug "#2 [" + RegularExpressionGroup(0, 2) + "]"
Wend    

FreeRegularExpression(0)

End

Code: Select all

#1 [319 811]
#2 [51 478]
But you don't say in which case you're taking the last or penultimate number as the second argument ?

Re: Help with regexp

Posted: Wed Aug 14, 2024 8:11 am
by loulou2522
The two case to test is
Define Txt$ = ". Emprunts 319 811 | 27,79 51 478 | 5.84"
Define Txt$ = ". 51 478 | 5.84"
in the second test 51478 will be in the same position as first test and in the first position 319 811 they will be nothing
In factj the second test will return group1 "" group2 57811
In this two case the expression must return two group. Is-it possible ?

Re: Help with regexp

Posted: Wed Aug 14, 2024 9:10 am
by Marc56us
To sum up : If the line starts with "Emprunts ", we take the first number (319 811) and the second to last (51 478).
Otherwise both (51 478) and (5.84) ? Easy, since there's only one separator (|)

From where 57 811 ?

PS. In general, exported files use a field separator which is rarely a space, especially if it is also the thousand separator.
Isn't the original separator a tab? Please use [ code ] tag for sample data and exact copy/paste.

To always use the same number of groups, simply create an empty group or use named groups.

:wink:

Re: Help with regexp

Posted: Wed Aug 14, 2024 11:30 am
by infratec
My 'Regular Expression":

Code: Select all

EnableExplicit


Structure Group_Structure
  Group1$
  Group2$
EndStructure


Procedure.i SeparateGroups(Txt$, *Group.Group_Structure)
  
  Protected Result.i, Help$, Pos.i
  
  
  
  If FindString(Txt$, "Emprunts")
    Help$ = RemoveString(Txt$, "Emprunts")
    Help$ = RemoveString(Help$, ".")
    
    *Group\Group1$ = RemoveString(StringField(Help$, 1, "|"), " ")
    Result = 1
    Help$ = Trim(StringField(Help$, 2, "|"))
    Pos = FindString(Help$, " ")
    If Pos
      Help$ = Mid(Help$, Pos)
      *Group\Group2$ = RemoveString(Help$, " ")
      Result + 1
    EndIf
  Else
    Help$ = RemoveString(Txt$, " ")
    Help$ = RemoveString(Help$, ".")
    *Group\Group2$ = StringField(Help$, 1, "|")
    If *Group\Group2$ <> ""
      Result = 1
    EndIf
  EndIf
  
  ProcedureReturn Result
  
EndProcedure



Define Group.Group_Structure
Define Txt$



If SeparateGroups(". Emprunts 319 811 | 27,79 51 478 | 5.84", @Group)
  Debug "1: " + Group\Group1$ + " 2: " +  Group\Group2$
EndIf

Group\Group1$ = ""
Group\Group2$ = ""
If SeparateGroups(". 51 478 | 5.84", @Group)
  Debug "1: " + Group\Group1$ + " 2: " +  Group\Group2$
EndIf


Re: Help with regexp

Posted: Wed Aug 14, 2024 1:19 pm
by Marc56us
Another (just for fun with RegEx)

Code: Select all

EnableExplicit

Procedure Extract_Values(Txt$, RegEx$)
    Protected Tmp_1$, Tmp_2$
    Debug "Txt: [" + Txt$ + "]"
    
    If Not CreateRegularExpression(0, RegEx$)
        Debug RegularExpressionError() : ProcedureReturn 
    EndIf
    
    If Not ExamineRegularExpression(0, Txt$)
        Debug "Nothing" : ProcedureReturn
    EndIf
    
    Debug "  Found: " + CountRegularExpressionGroups(0)
    
    While NextRegularExpressionMatch(0)
        Tmp_1$ = RemoveString(RegularExpressionGroup(0, 1), " ")
        Tmp_2$ = RemoveString(RegularExpressionGroup(0, 2), " ")
        Debug "      #1 [" + Tmp_1$ + "]"
        Debug "      #2 [" + Tmp_2$ + "]"
    Wend    
    
    FreeRegularExpression(0)
    Debug ""
EndProcedure

Extract_Values(". Emprunts 319 811 | 27,79 51 478 | 5.84", 
               "(\d[\d ]+\d).+? (\d[\d ]+\d)")

Extract_Values(". Emprunts 51 478 | 5.84",
               "() ([\d ]+) \|")

End

Code: Select all

Txt: [. Emprunts 319 811 | 27,79 51 478 | 5.84]
  Found: 2
      #1 [319811]
      #2 [51478]

Txt: [. Emprunts 51 478 | 5.84]
  Found: 2
      #1 []
      #2 [51478]
:wink:

Re: Help with regexp

Posted: Thu Aug 15, 2024 9:05 pm
by loulou2522
here is all the case i want to test
chaine1.s="378 632| 7,96 468 002 | 22:56""
chaine1.s = chaine1.s="1 378 632| 7,96 1 468 002 | 22:56"
chaine1.s = chaine1.s="1632| 7,96 1 468 002 | 22:56"
My expression seems working with rexman and don't work with purebasic

Code: Select all

chaine1.s=" 1 378  632| 7,96               468 002   | 22:56"

 If CreateRegularExpression(0, "([-| ]\d{1,3}\s{0,9}\d{1,3}.\d{1,3}\s{0,9})|(\d{1,4}\s{0,}[|])", #PB_RegularExpression_Extended)
  Dim Result$(0)
  NbFound = ExtractRegularExpression(0, chaine1, Result$())
      ;  Debug nbfound
    If nbfound >0
      For i=0 To NbFound-1
        If i=0
          result$(i)=  ReplaceString(result$(i),"|",Space(0))
          ;    Debug chaine1
          ;   Debug result$(i)
          mont11.s =ReplaceString(result$(i), Chr(32), Space(0))
        ElseIf i=2
          result$(i)=  ReplaceString(result$(i),"|",Space(0))
          mont22.s =ReplaceString(result$(i), Chr(32), Space(0))
        EndIf 
      Next 
      FreeRegularExpression(0)
      FreeArray(result$())
    Else 
   ;   Debug RegularExpressionError()
    EndIf
  EndIf   
  Debug mont11
  Debug mont22
thanks for help

Re: Help with regexp

Posted: Thu Aug 15, 2024 11:17 pm
by AZJIO

Code: Select all

EnableExplicit
#re = 0
Define NbFound, txt$, i
Define Dim Result$(0)
txt$ = ". Emprunts 319 811 | 27,79 51 478 | 5.84"

If CreateRegularExpression(#re, "(?<=\h)[\d\h]+?(?=\|)")
	NbFound = ExtractRegularExpression(#re, txt$, Result$())
	For i = 0 To NbFound - 1
		Result$(i) = ReplaceString(Result$(i), " ", "")
		Debug Val(Result$(i))
	Next
EndIf

Re: Help with regexp

Posted: Fri Aug 16, 2024 5:01 am
by loulou2522
Thanks that's work.
But number can be negative. How can i integrate this constraint
example of test
txt$ = ". Emprunts -811 | 27,79 51 478 | 5.84"
thanks

Re: Help with regexp

Posted: Fri Aug 16, 2024 5:08 am
by AZJIO

Code: Select all

(?<=\h)-?[\d\h]+?(?=\|)")

Re: Help with regexp

Posted: Sat Aug 17, 2024 5:08 pm
by loulou2522
I have another problem with that's regexp
the case if the following
txt$ = ". Emprunts -811 | 779 51 478 | 5.84"
I want to test the var 779 that will be obligatory like 7.79 or 7.79
With the regexp (?<=\h)-?[\d\h]+?(?=\|)")
it return 811 and 77951478 in fact what i wait is 779 , 51478, i would want to put a comma on 778 like 7,78 becaus this number is a percent in case of the comma is not present
Thanks
Maybe it's not possible with regexp

Re: Help with regexp

Posted: Sat Aug 17, 2024 5:17 pm
by AZJIO

Code: Select all

(?<=\h)(-?[\d\h]+?)(?:\|\h?[\d,.]+?)