PeteEngineer
PeteEngineer

Reputation: 113

Regex not working

I need a regex for allowing only numbers + following characters (+, , ( ,) )

I have done this :

^-{0,1}\d+$

But this is taking only numbers ..how we can write it for the charecters as well?

Upvotes: 1

Views: 109

Answers (3)

stema
stema

Reputation: 92976

What you need is a character class. You can define one with square brackets []. Then just add all those characters, ranges or predefined shorthand classes to your character class.

[-+\d() ]

Such a character class will then match exactly one character out of the class members.

You may want to read more details about character classes on regular-expressions.info or a real brief overview about basic regex features on my blog post "What absolutely every Programmer should know about regular expressions"

Upvotes: 0

tomsv
tomsv

Reputation: 7277

Try the following:

^[0-9-.+ ()]+$

Upvotes: 2

Vishal Suthar
Vishal Suthar

Reputation: 17193

This would be the solution:

^[0-9\+\s\(\)]+$

Regex Demo

Upvotes: 2

Related Questions