Chirayu
Chirayu

Reputation: 4901

regular expression for some special charaters and number in php

I am looking for a php regex that can contain only , and - chracters.

in short i would like a regex that can match the following:

* may contain number
* may contain space
* may contain , -

i am making a php script to check if a certain string have the above or not, like a validation check. i have tried with this

^([\[\]=,{\}\0-9_-]+)$

but not working. please someone help me.

Upvotes: 1

Views: 129

Answers (4)

Rajesh purohit
Rajesh purohit

Reputation: 178

preg_match("#-?\d+(,\d+)?#", "-1,2", $match);

Upvotes: 1

Saeed
Saeed

Reputation: 7370

Use this:

^([\ ,0-9-]*)$

I offer you using this site to see what your regex expose.

Upvotes: 0

Álvaro González
Álvaro González

Reputation: 146450

The - literal must be the first or last character in character classes. Otherwise, it's a range indicator:

^[0-9 ,-]+$

(Not sure though why you add = and other stuff.)

Upvotes: 3

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56162

Try this regular expression:

^[\d\s,-]*$

Upvotes: 1

Related Questions