B4J Question regex for split this string:10.8+5-3

Roberto P.

Well-Known Member
Licensed User
Longtime User
can someone help me to extract the rows from the value indicated in the object.
target:
10.8+
3+
5
result:
10.8
3
5
I hope it is clear

Thanks in advance
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
Hi
the numbers are in the same row. In this case doesn't work. Show only the first row
thank you
 
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
B4X:
Dim m As Matcher = Regex.Matcher2("([\d\.]+)",Regex.MULTILINE, s)
ok, I try to explain better:

from this string:

10.8 + 5 + 3

I have to extract 3 strings

10.8
5
3

this string represents a cumulative discount
I hope it is clear


thank you
 
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
I solved it with integration of the hand sign. It is used to calculate the multiple discount in the management systems: 10 + 4 + 5.

thank


Regex:
Dim m As Matcher = Regex.Matcher2("[\d\.]+", Regex.MULTILINE, strFormula)
    
     
    Dim aSegno As String
    
    Do While m.Find
        Dim aPos As Int    = strFormula.IndexOf(m.Match)
 
 
        
        If aPos = 1 Then
            aSegno = strFormula.CharAt(0)
        Else
            Dim aStart As Int     = aPos -1
            Dim aLun As Int     = m.Match.Length
            aSegno                 = strFormula.SubString2(aStart, aLun+aStart)
        End If
        
        If aSegno = T_PLUS Then
            dDebitCreditSign = 1.0
        else if  aSegno = T_MINUS Then
            dDebitCreditSign = -1.0
        End If
        
        If Not(bIsFirstDiscount) And dFromToDiscount = 100.00 Then
            dFromToDiscount = dFromToDiscount - aData.mPerc1
        End If
        
        ...
        loop
 
Upvote 0

Xfood

Expert
Licensed User
sorry, I did a simple test like this, I think it's fine
B4X:
Dim Sconti As String ="10+3+7+11.5+26"
    
    Dim Lines() As String = Regex.Split("\+", Sconti)
    
    For Each Line As String In Lines
        Log(Line)
    Next
1625849807530.png
 
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
Hi
yes, it works, but I must also be able to read the minus sign, example:

10.8 + 5-3.2
or
-10 + 5

the ideal function would be to be able to extract the complete sign string, like this:

+10.8
+5
-3.2

I didn't succeed!

thank you
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
if you need to evaluate an expression use this code from Erel

 
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
if you need to evaluate an expression use this code from Erel

I read it but I'm not able to find solution
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Not pretty but this seems to work:

B4X:
Dim S As String ="10+3+7-11.5+26"
    
    Dim Lines() As String = Regex.Split("\+|\-", S)
    
    Dim values(6) As String
    Dim t As Int
    Dim i As Int
    
    Log(S)
    Log("")
    For Each Line As String In Lines
        t=S.IndexOf(Line)
        If t>0 Then
            If S.CharAt(t-1)="-" Then
                values(i)="-" & Line
            Else
                values(i)=Line
            End If
        Else
            values(i)=Line
        End If   
        Log(values(i))
        i=i+1
    Next

Output (s):

Waiting for debugger to connect...
Program started.
10+3+7-11.5+26
10
3
7
-11.5
26


Waiting for debugger to connect...
Program started.
10+3-7.02-11.5+26.5
10
3
-7.02
-11.5
26.5

Waiting for debugger to connect...
Program started.
-9.8+3-7.02-11.5+26.5
-9.8
3
-7.02
-11.5
26.5


Regards
 
Upvote 0
Top