B4J Question Regex for (simple) mathematical expression

Chris2

Active Member
Licensed User
I'm trying to come up with a set of regex checks to validate a string that should be a valid mathematical expression.
I'm limiting the expression to include:
The 4 standard operators: +, -, *, /
Curly brackets: ( and )
Integers and decimals.

So I'm not allowing any functions like min, max, sin, tan, etc.

So far I have:
Validate mathematical expression with regex:
Private Sub CalcRegexCheckOK (s As String) As Boolean
                                                             
    Dim lst As List = Array As String( _
    $"[^\d()\*\/\+\-\.]"$, _            'basic allowed characters (negated match with ^)
    $"\A[\/\*\)\.]"$, _                    'not allowed at start of string
    $"[\+\-\/\*\(\.]\z"$, _                'not allowed at end of string
    $"(\-|\*|\/|\+|\(|\)|\.)\1"$, _        'duplicate individual characters
    $"[\+\-\*\/\.]{2,}"$, _                'invalid consecutive characters
    $"[\+\-\*\/\.\(]\)"$, _                '+ - * / . ( can't go directly befe )        
    $"[\.\)]\("$, _                     '. and ) can't go directly before (                    
    $"\d+\.\d+\.\d+"$)                 'nonsense 'double' decimals like 112.23.34                
    
    For Each pattern As String In lst
        
        Dim m As Matcher = Regex.Matcher(pattern, s)
        If m.Find Then Return False
        
    Next
                                    
    Return True
 
End Sub

Regex can be a bit opaque to me so I'm more interested in making it readable/understandable rather then concatenating everything into a single regex pattern.

Can anyone spot anything I'm missing or that I've got wrong?

Thanks.

Edit: changed the CalcRegexCheckOK sub because I got very confused between Regex.IsMatch and Regex.Matcher :rolleyes:
 
Last edited:

MicroDrie

Well-Known Member
Licensed User
You can test regex expressions here

Your 123+(123/456)*(365.98/42.25)-2.321 could be tested with /[0-9A-Za-a]*( ){0,}([+-/*]( ){0,}[0-9A-Za-a]*( ){0,})*/gm
 
Upvote 0

Chris2

Active Member
Licensed User
Thanks @MicroDrie, but I'm not trying to match that exact sum. I'm trying to test for a string that is an invalid sum/expression for some reason.
I will edit my original post and remove the example sum to prevent confusion.
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
I'm not trying to match that exact sum. I'm trying to test for a string that is an invalid sum/expression for some reason.
An acceptable choice Chris2, let's do some flip-thinking. The goal is not the result of the calculation, but whether the method of calculation is correct. However, only a correct function can produce a result. Yes, a detour, but detecting an error :mad: in the result of a regex function is also a determination that the method of calculation is not correctšŸ˜….
 
Upvote 0

Chris2

Active Member
Licensed User
Edit: I've changed the sub I have so far in the first post because I'd gotten confused between Regex.IsMatch & Regex.Matcher.
 
Upvote 0
Top