2

I want to construct a pattern that identifies the valid domain name. A valid domain name has alphanumeric characters and dashes in it. The only rule is that the name should not begin or end with a dash.

I have a regular expression for the validation as ^\w((\w|-)*\w)?$

However the expression is validating the strings with underscores too (for ex.: cake_centre) which is wrong. Can anyone tell me why this is happening and how it can be corrected?

P.S.: I am using preg_match() function in PHP for checking the validation.

shiv
  • 191
  • 2
  • 11

3 Answers3

4

The metacharacter \w includes underscores, you can make a character class that will allow your listed requirements:

[a-zA-Z\d-]

or per your regex:

^[a-zA-Z\d]([a-zA-Z\d-]*[a-zA-Z\d])?$

(Also note the - position in the character class is important, a - at the start or end is the literal value. If you have it in the middle it can create a range. What special characters must be escaped in regular expressions?)

chris85
  • 23,846
  • 7
  • 34
  • 51
0

Underscores are being validated because they are part of the \w character class. If you want to exclude it, try:

/^[a-z0-9]+[a-z0-9\-]*[a-z0-9]+$/i
BeetleJuice
  • 39,516
  • 19
  • 105
  • 165
0

Here is the regexp with lookaround approach

 (?<!-)([a-zA-Z0-9_]+)(?!-)

regexp pattern is created in 3 groups

First group ^(?<!-) is negetive look back to ensure that matched chars does not have dash before

Second group ([a-zA-Z0-9_]+) give matching characters

Third group (?!-) is negetive lookahead to ensure match is not ending with dash 
Noman Khan
  • 920
  • 5
  • 12