Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match two or more dots in a string using regular expression

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?

like image 443
vkGunasekaran Avatar asked Feb 14 '23 14:02

vkGunasekaran


2 Answers

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.

like image 119
utdemir Avatar answered May 19 '23 06:05

utdemir


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.

like image 28
Daniël Knippers Avatar answered May 19 '23 04:05

Daniël Knippers