user2203703
user2203703

Reputation: 2005

catch subdomain that have exact format

I'm trying to catch every hostname that have the exact format:

letters(+or)numbers.example.com

My regex:

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

It should work like:

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

How i can do it please?

Upvotes: 0

Views: 44

Answers (2)

Jan
Jan

Reputation: 43169

You could use

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

See a demo on regex101.com.


Broken down this says

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

Upvotes: 3

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Change the quantifier from * to + and add word boundary \b:

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

Upvotes: 2

Related Questions