Finding and replacing brackets in a string in Elm 0.16

I am currently trying to find opening parentheses in a string using a regular expression in Elm 0.16 and replace each one with a bracket followed by one place. I also plan to replace each of the closing parentheses in the string with a space followed by a closing bracket. Therefore, I can replace the spaces with a comma to separate the line. The line I'm trying to use regex on is here:

((data "quoted data" 123 4.5) (data (! @ # (4.5) "(more" data "))))

I already used regex to remove any backslashes used to exclude quotes. For this, I used this function:

getRidOfBackslashes : String -> String getRidOfBackslashes sExpression = sExpression |> Regex.replace Regex.All (Regex.regex "\\g") (\_ -> "") 

Then I tried to use the following function to achieve the previously stated goal regarding opening parentheses:

 createSpacesParentheses sExpression = sExpression |> (\_ -> getRidOfBackslashes sExpression) |> Regex.replace Regex.All (Regex.regex "\(") (\_ -> "( ") 

Looking at different jayscript controllers, my very simple regex seems to do what I want, but the Elm compiler gives me an error:

 (line 1, column 3): unexpected "(" expecting space, "&" or escape code 27│ |> Regex.replace Regex.All (Regex.regex "\(") (\_ -> "( ") ^ Maybe <http://elm-lang.org/docs/syntax> can help you figure it out. 

I am wondering if I will do it right, and if someone can offer help. Thank you in advance.

+5
source share
1 answer

See this link :

Be careful to avoid backslashes! For example, "\w" avoids the letter w , which is probably not what you want. You probably want "\\w" instead, which eludes a backslash.

Thus, a simple way is to simply use the character class [(] (as inside the character class, all the "special" characters, but \ , ] , ^ and - lose their special meaning), or you can use \\( .

+4
source

Source: https://habr.com/ru/post/1242324/


All Articles