Regex expression

Hello i try to use a regex first time in ELM, could you please help me to complete this statement

getPolishSumHelper : List String -> List String -> String
getPolishSumHelper inputs digitsStack =
    let
        _ = Debug.log "inputs" inputs
        _ = Debug.log "digitsStack" digitsStack
    in
    case ( inputs, digitsStack ) of
        ( [], topOfStack :: _ ) -> if topOfStack == Regex.fromString"([^0-9]?)" then "ERROR" else topOfStack
        

        ( "+" :: rest, first :: second :: stack ) ->
            calcNext (+) rest first second stack

        ( "-" :: rest, first :: second :: stack ) ->
            calcNext (-) rest first second stack

        ( "*" :: rest, first :: second :: stack ) ->
            calcNext (*) rest first second stack

        ( "/" :: rest, first :: second :: stack ) ->
            if second == "0" then "0" else calcNext (/) rest first second stack

        ( number :: rest, stack ) ->
            getPolishSumHelper rest <| number :: stack
            

        _ ->
            "ERROR"

You are tying to compare a Maybe Regex with a String this will not work in any language:

topOfStack == Regex.fromString"([^0-9]?)"

What you probably want is to create a regex and then check if it contains a string?

If you look at the regex documentation here

You will see that the function fromString accepts a String and returns a Maybe Regex

fromString : String -> Maybe Regex

There is also an example in the docs - you should read this carefully. (The docs are of high quality and you should read them first when you get stuck with a function or module)

Also look at the containsdocumentatin - it might be what you want ?!

I try to check if the top of the stack is à caracter “a-Z” or number “0-infinity”

In your second to last case branch you have the variable number. You can use the function String.toInt number to check if it’s a number. To see if it is a single letter you can use String.uncons number to see if it is a single Char, by doing something like

case String.uncons number of
    Just ( firstChar, [] ) -> -- just a single Char
        Char.isAlpha firstChar -- returns a Bool of whether or not the Char is in-zA-Z
    _ -> -- either empty or multiple Char

thank you for your answer, i resolve it with :

( [], topOfStack :: _ ) -> if String.toInt topOfStack == Nothing || length digitsStack > 1 then "ERROR" else topOfStack

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.