Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression, How to allow combination of dot (period) and letters?

Tags:

regex

I want to allow (.) and (a-zA-Z) letters and _ and - , I have some problems with the (.) ,

Any idea ?

Thanks in advance ,

Ish

like image 757
politrok Avatar asked Jun 07 '10 09:06

politrok


3 Answers

[A-Za-z_.-]

is a character class that includes all the characters you mentioned. Inside a character class, it's not necessary to escape the ., and you can avoid escaping the - if you put it first or last.

If numbers are ok, too, you can shorten this to

[\w.-]
like image 133
Tim Pietzcker Avatar answered Oct 01 '22 22:10

Tim Pietzcker


This will do [a-zA-Z_.-]+

Outside the character class, ([]), you need to escape the dot (\.)as it is a meta character.

[a-z]+\.com  #matches `something.com`
like image 38
Amarghosh Avatar answered Oct 01 '22 20:10

Amarghosh


[a-zA-Z_\-.] should work. You might have to use a double slash, depending on the language you are using.

like image 20
npinti Avatar answered Oct 01 '22 21:10

npinti