I need to a regex to validate a string like "foo.com"
. A word which contains a dot. I have tried several but could not get it work.
The patterns I have tried:
(\\w+\\.)
(\\w+.)
(\\w.)
(\\W+\\.)
Can some one please help me one this.
Thanks,
You can then use \. \. or \. {2} to match exactly 2 dots.
JavaObject Oriented ProgrammingProgramming. The subexpression/metacharacter “.” matches any single character except a newline.
If you want the dot or other characters with a special meaning in regexes to be a normal character, you have to escape it with a backslash. Since regexes in Java are normal Java strings, you need to escape the backslash itself, so you need two backslashes e.g. \\.
Java Example to Split String by Dot The examples are pretty much similar to splitting String by any delimiter, with only a focus on using the correct regular expression because the dot is a special character in Java's regular expression API. That's all about how to split a String by dot in Java.
Use regex with character class
([\\w.]+)
If you just want to contain single .
then use
(\\w+\\.\\w+)
In case you want multiple .
which is not adjacent then use
(\\w+(?:\\.\\w+)+)
This regex works:
[\w\[.\]\\]+
Tested for following combinations:
foo.com
foo.co.in
foo...
..foo
To validate a string that contains exactly one dot and at least two letters around use match for
\w+\.\w+
which in Java is denoted as
\\w+\\.\\w+
I understand your question like, you need a regex to match a word which has a single dot in-between the word (not first or last).
Then below regex will satisfy your need.
^\\w+\\.\\w+$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With