I'm really sorry to ask this question but I just can't work it out.
How do I check if a string contains only: numbers '0-9', spaces ' ', hyphens '-', dots '.'
If it contains any other character it will return false;
I have this at the moment:
Suffice to say.. it doesn't work :P
Thanks for everyones help :)
There's two issues with that:
Firstly, \w behaves weirdly. I'm not sure of its mechanics, but just replace it with a space.
Second, you need to account for multiple matches, with a + sign.
Your result should be:
Zack thank you so much.
The "+" was what I was looking for I think :) I didn't get the multiple matches business lol.
This forum is fantastic. Thank you again.
Another way, which should be more efficient (not tested) is attempt to find the negation of the acceptable characters. Not found [!matcher.find()] is a pass of your conditions. The regex would beNote that the dot [.] is not a metacharacter when within a character class, and a dash/hyphen [-] has no special interpretation at the beginning or end of a character class, so neither of them need to be escaped.
That said, it's sometimes wise to retain unnecessary escapes to make the regex more readable to someone less familiar with these nuances ;)db