2017-10-16 108 views
1

我试图抓住每一个主机名有确切的格式:抓子域有确切格式

letters(+or)numbers.example.com 

我的正则表达式:

$pattern = '/([0-9a-z]*\.)example.net/'; 

它应该像:

test2155.example.net // Catch 
2155.example.net // Catch 
Test2155.example.net // Don't Catch 
test2155.example.net655 // Don't Catch 
[email protected] // Don't Catch 
[email protected]@655 // Don't Catch 

我该怎么做才好?

+1

变化'*''到+',并添加lookarounds检查主机名是由你想要的分隔符(空格,该字符串的限制,...) –

+1

如何包围关于'([0-9a-z] + \。)example.net' – developer

+0

这个'[0-9a-z]'不会与'5V444.example.net'的'5V444'匹配。它是否被考虑? – RomanPerekhrest

回答

2

变化从*量词来+并添加单词边界\b

/([0-9a-z]+\.)example.net\b 
+0

对不起,我更新了我的帖子。 – user2203703

3

你可以使用

([a-zA-Z0-9]+)\Q.example.net\E 

a demo on regex101.com


分布看此说

([a-zA-Z0-9]+) # your original expression 
\Q.example.net\E # .example.net literally 
+0

单词边界是无用的,并不能证明任何东西。例如:'abc-123.example.net' –

+0

@CasimiretHippolyte:OP不想破折号。 – Jan

+0

确实,但你的模式将返回'123.example.net'。 –