James Hay
James Hay

Reputation: 7325

Simple Regex help needed

I need to create a regular expression to match a string that contains anything other than the specified characters. The characters are

a-z A-Z 0-9 + - * / : . $ % and a space

I'm not very familiar with regex so I'm unsure how to put it together and test it. I can find lots of cheat sheets but I don't know how to actually structure it as one whole pattern.

Upvotes: 2

Views: 69

Answers (2)

Jamiec
Jamiec

Reputation: 136104

a ^ in a capture group character class negates those characters in the class. So:

[^a-zA-Z0-9+\-*/:.]

Some characters there are special chars in regex so they're escaped with \.

Upvotes: 6

jhavrda
jhavrda

Reputation: 395

~^[^a-z0-9\+\-\*\/\:\.\$\%\x20]*$~i

Starting with ^ and ending with $ to make sure that string contains only allowed characters. Character group is starting with ^ for negation. \x20 stands for space, to much any whitespace use \x20. This RegExp is case insensitive (i modifier). You may test your regular expressions here http://regex.larsolavtorvik.com/

Upvotes: 0

Related Questions