I’m a Java user but I’m new to regular expressions.
I just want to have a tiny expression that, given a word (we assume that the string is only one word), answers with a boolean, telling if the word is valid or not.
An example… I want to catch all words that is plausible to be in a dictionary… So, i just want words with chars from a-z A-Z, an hyphen (for example: man-in-the-middle) and an apostrophe (like I’ll or Tiffany’s).
Valid words:
"food""RocKet""man-in-the-middle""kahsdkjhsakdhakjsd""JESUS", etc.
Non-valid words:
"gipsy76""www.google.com""me@gmail.com""745474""+-x/", etc.
I use this code, but it won’t gave the correct answer:
Pattern p = Pattern.compile("[A-Za-z&-&']");
Matcher m = p.matcher(s);
System.out.println(m.matches());
What’s wrong with my regex?
+after the expression to say “one or more of those characters”:\(or put it last).&characters:Here’s the code:
Complete test:
(Produces no output.)