1

I tried this regex syntax in http://gskinner.com/RegExr/ and seem working well.

^[a-z0-9-]+.([a-z]{2,4})$

the purpose is to match domain name(not containing http or https)

so i use preg_match( '/^[a-z0-9-]+.([a-z]{2,4})$/g', 'fathi-hadi.net' ) but always return false

i don't know why

2
  • 3
    you may want to escape the . => \., the dot matches any character. Commented Jan 10, 2013 at 14:57
  • yes, you are right, thanks to mention Commented Jan 10, 2013 at 14:58

1 Answer 1

2

When you turn on error reporting, you would've noticed preg_match doesn't support the g modifier:

Warning: preg_match() [function.preg-match]: Unknown modifier 'g'

So drop that and use:

preg_match('/^[a-z0-9-]+\.([a-z]{2,4})$/', 'fathi-hadi.net');

That will match. I also incorporated Gerald Schneider's comment about escaping the dot, which is a good point.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, it match now, Thanks Oldskool
There is still a bug with the dot -- as it is now, it will match any character, which isn't the intended result.
@SDC : yes, in the first comment, Gerald Schneider already mention about that. Thanks you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.