开发者

Simple Regex help needed

I need to create a regular expression to match a string that contains anything other than the specified characters. The char开发者_JAVA技巧acters 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.


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 \.


~^[^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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜