Reputation: 6814
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
?
Upvotes: 5
Views: 28999
Reputation: 9
Pattern.matches("[^\\.]+\\.[^\\.]+", "any_String")
Upvotes: -1
Reputation: 22820
Try this one :
(.*\.)+.*
or this one (to specifically match "characters" and just... anything) :
(\w*\.)+\w*
Demo :
Upvotes: 2
Reputation: 3055
This is with JavaScript.
var regex = /(\..*){2,}/;
regex.test("hello.world."); // true
regex.test("hello.world"); // false
regex.test("."); // false
regex.test(".."); // true
It searches for the pattern 'dot followed by anything (or nothing)', repeated 2 or more times.
Upvotes: 5
Reputation: 1510
^[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.
Upvotes: 0
Reputation: 27266
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.
Upvotes: 5