I want to create to regular expression that allows all combinations of characters and digits and also dots but not a dot at the beginning. So valid strings could be "1.2.3" or "1.b.34d" or something. But I want to disallow strings like "." or ".1.2.3".
I'm trying with
You may use
The pattern will match a string that does not start with a
. due to the negative lookahead
(?!\\.) and contains zero or more word chars (letters, digits or underscores -
\w), whitespaces (
- symbols. Note you do not need
\z since the
String#matches() method anchors the pattern by default.
NOTE: To make this pattern Unicode-aware, add
Pattern.UNICODE_CHARACTER_CLASS embedded flag option) at the start of the pattern: