#Regex for accepting only braces

27 messages · Page 1 of 1 (latest)

unborn brook
#

Hi everyone, I just started to learn go and came across this challenge on how to check if braces are valid or not.
example:
([{}]) -> true
((]}) -> false
{{}) -> false
(){}[()] -> true
I kinda get the idea on how I'm going to make the function. But, first of all I need to check if the input string are valid (means it only contains braces). I've red the documentation about go regex, but could not fully understand it yet. How do you translate this -> "only (, {, [, ], }, ) are allowed" into go regex?

pine topaz
#

i would think about the problem a bit more deeply because you definitely do not need regex for this

#

in fact i would go as far to say that it is the incorrect solution

unborn brook
pine topaz
#

is what you want

pine topaz
#

the regex is the same

unborn brook
#

it's a bit different from what i've read in go documentation

pine topaz
#

more or less

#

the important bits you need here are the same

#

that allows you to say "the set of these characters"

unborn brook
pine topaz
#

^ at the beginning says "begins with"

#

$ at the end says "ends with"

#

then (x)+ says 1 or more of x capture group (your character set

unborn brook
pine topaz
#

is an empty string valid

unborn brook
#

owh.. empty string still valid tho 😕

#

need to read it again

pine topaz
#

* is 0 or more

unborn brook
pine topaz
unborn brook
pine topaz
#

and set it to golang