# Regex expression

**URL:** <https://discourse.elm-lang.org/t/regex-expression/7669>\
**Category:** Learn\
**Created:** [August 12, 2021, 7:37pm UTC](https://discourse.elm-lang.org/t/regex-expression/7669 "2021-08-12T19:37:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zack\_slm](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/zack_slm/32/4166_2.png) [@Zack\_slm](https://discourse.elm-lang.org/u/Zack_slm)\
**Post date:** [August 12, 2021, 7:37pm UTC](https://discourse.elm-lang.org/t/regex-expression/7669/1 "2021-08-12T19:37:55Z")

</div>

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

```auto
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"

```

---

<div class="post-metadata">

**Author:** ![grigorious](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/grigorious/32/3549_2.png) [@grigorious](https://discourse.elm-lang.org/u/grigorious)\
**Post date:** [August 12, 2021, 7:46pm UTC](https://discourse.elm-lang.org/t/regex-expression/7669/2 "2021-08-12T19:46:02Z")

</div>

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

```elm
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](https://package.elm-lang.org/packages/elm/regex/latest/Regex#fromString)

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

```elm
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 `contains`documentatin - it might be what you want ?!

---

<div class="post-metadata">

**Author:** ![Zack\_slm](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/zack_slm/32/4166_2.png) [@Zack\_slm](https://discourse.elm-lang.org/u/Zack_slm)\
**Post date:** [August 12, 2021, 7:49pm UTC](https://discourse.elm-lang.org/t/regex-expression/7669/3 "2021-08-12T19:49:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wolfadex](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/wolfadex/32/2461_2.png) [@wolfadex](https://discourse.elm-lang.org/u/wolfadex)\
**Post date:** [August 12, 2021, 9:50pm UTC](https://discourse.elm-lang.org/t/regex-expression/7669/4 "2021-08-12T21:50:09Z")

</div>

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

```elm
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

```

---

<div class="post-metadata">

**Author:** ![Zack\_slm](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/zack_slm/32/4166_2.png) [@Zack\_slm](https://discourse.elm-lang.org/u/Zack_slm)\
**Post date:** [August 13, 2021, 9:39am UTC](https://discourse.elm-lang.org/t/regex-expression/7669/5 "2021-08-13T09:39:12Z")

</div>

thank you for your answer, i resolve it with :

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

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [August 23, 2021, 9:39am UTC](https://discourse.elm-lang.org/t/regex-expression/7669/6 "2021-08-23T09:39:23Z")

</div>

Closed on August 23, 2021, 9:39am UTC

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