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?
#Regex for accepting only braces
27 messages · Page 1 of 1 (latest)
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
I actually solved it without regex, but then I'm just curious about regex in go 😄
is what you want
isn't this javascript?
the regex is the same
it's a bit different from what i've read in go documentation
more or less
the important bits you need here are the same
In a regular expression, square brackets are used to indicate character classes, which matches any of the characters in the character set.
that allows you to say "the set of these characters"
I will look into this, thankyou for sharing this
^ 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
i get it now. here is the code I came up with
var text = "[{()}]9"
var regex, _ = regexp.Compile(^[\[\{\(\)\}\]]*$)
var isMatch = regex.MatchString(text)
fmt.Println(isMatch)
thank you for your support! really appreciate it
is an empty string valid
* is 0 or more
oh I see.. it's good now. thank you
this tool makes it a lot more easier to understand
sorry you actually want https://regex101.com/
and set it to golang