dotty
dotty

Reputation: 41433

php regex for string containing a "." and "-"

Hay all, i need help making a regex. The string must contain a "-" and must not contain a ".".

Can someone help me please.

Upvotes: 0

Views: 168

Answers (3)

Franz
Franz

Reputation: 11553

You don't need a regex for that:

if (strpos($string, '-') !== false && strpos($string, '.') === false)
    //do what you want...

Upvotes: 13

ghostdog74
ghostdog74

Reputation: 342363

no need regex for this, one method is to use strpos

strpos($mystr,"-" ) !== FALSE && strpos($mystr,"." ) === FALSE

Upvotes: 2

Kobi
Kobi

Reputation: 138017

Don't know about php, but this should do it:

^[^.]*-[^.]*$

Upvotes: 3

Related Questions