I need to capture strings containing more than one dot. String will mostly contains domain names like example.com, fun.example.com, test.funflys.com.
How can I do this using regex?
I need to capture strings containing more than one dot. String will mostly contains domain names like example.com, fun.example.com, test.funflys.com.
How can I do this using regex?
You should escape dot's because they have special meaning.
So, that regex would be;
.*\..*\..*
But you should be careful that \ is possibly have a special meaning on your programming language too, you may be have to escape them also.
Try this one :
(.*\.)+.*
or this one (to specifically match "characters" and just... anything) :
(\w*\.)+\w*
Demo :
^[A-Za-z0-9]*([A-Za-z0-9]*|\.+)+$
This will capture words or digits followed by at least one . and that can be repeated as many times.
Would be captured: hello.world.something hello......world.world something.09hello...hello
If you provide some more examples on what can and what can't be captured we can update this regex.
Pattern.matches("[^\\.]+\\.[^\\.]+", "any_String")
a.b for example. OP also said nothing about leading or trailing dots, why do you think strings with those should be rejected?Pattern.matches("[^\\.]*\\.[^\\.]*\\.[^\\.]*", "any_String")