#RegEx in Java making my head hurt

18 messages · Page 1 of 1 (latest)

slim parcel
#

I am trying to use the String.matches() method to check if a string is 4 character long and only contains the character "RGBY".
In Java however, it always returns false even when it looks like it should be true.
This is my regex "/[RBGY]{4}/", which works outside of Java. Any ideas? Ty

subtle escarpBOT
#

This post has been reserved for your question.

Hey @slim parcel! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

subtle escarpBOT
surreal crown
#

/ shouldn't be part of the regex

slim parcel
#

oh

#

:)

surreal crown
#

/ are delimiters, like "
they aren't part of the regex's content
java doesn't have regex literals, so / isn't used

#

make sure you're choosing the java flavor when you test your regexes

slim parcel
#

Thank you so much! I was having such a hard time, I really appreciate it

subtle escarpBOT
surreal crown
#

note the quotes here

#

and when copied: "a"gm
no slashes

slim parcel
#

Which tool are you using?

surreal crown
#

(the gm flags are a separate thing, they don't work like this, but they do for regex literals; this is probably a bug)

surreal crown
slim parcel
#

Thank you so much, @surreal crown You're a life saver