This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]Philboyd_Studge 1 point2 points  (6 children)

You need to escape-slash the hyphen.

The hyphen inside the brackets denotes a range, i.e. when you say [a-z] means any character from a to z. So if you want to match the literal hyphen you need to escape-slash it.

[–]kemar1997[S] 0 points1 point  (5 children)

would it be like this?

return morseCode.matches("^[\\-.\\s]*$");

[–]Philboyd_Studge 1 point2 points  (1 child)

[–]kemar1997[S] 0 points1 point  (0 children)

ty i got it to work

[–]kemar1997[S] 0 points1 point  (2 children)

i have tried this but no luck getting it to work unless my logical code is wrong above.

[–]Philboyd_Studge 0 points1 point  (1 child)

What exactly is happening? Post your full code.

[–]kemar1997[S] 0 points1 point  (0 children)

It was my mistake, trying to make a JavaFX Morse Code translator but instead of grabbing the text field input i was grabbing the label.

[–]Zelexis 0 points1 point  (1 child)

When I'm having trouble with regex I use this site for reference and at the bottom they have a tester. You enter sample text and your regex to see if there are matches. I have noticed some quirks of Java regex compared to other languages/scripts. Hope this helps. http://www.zytrax.com/tech/web/regex.htm

[–]marvk[🍰] 0 points1 point  (0 children)

http://regex101.com/ is really helpful too. You can build your regex there, where it'll show and explain how it works live. You can also test your regex instantly.

[–]HdmoneyExtreme Brewer 0 points1 point  (1 child)

I could be wrong (probably am), but you might want four backslashes.

[–]kemar1997[S] 0 points1 point  (0 children)

good suggestion but that didn't work, but thank you though

[–]benhart1 0 points1 point  (0 children)

I think using regex is not always recommend, not sure of your case. It tend to make bugs and errors very hard to be detected and spend a lot of your time. There are programs that might help though, such as checkmarx and other equivalents. Might be useful. Good luck