views:

31

answers:

1
+1  Q: 

regex help please

Hello Guys need a quick Regular expression to match the following

A-Z a-z 0-9 " - ? . ', ! new lines and spaces

Any ideas?

+3  A: 

This will match any single one of those characters:

[A-Za-z0-9"?.',! \n\r-]

There's a good chance you want something like:

^[A-Za-z0-9"?.',! \n\r-]+$

Or possibly a bit simpler will meet your needs:

^[\w\s"?.',!-]+$

Remembering that if this is inside a string, you will need to escape either the " or ' in that (either by doubling up, or by prefixing with a backslash).

Also note that the - is last so that it is not treated as a range inside the character class. (Can also be placed first, or prefixed with backslash to prevent that).

The \w will match a "word" character, which is almost always [A-Za-z0-9_].
The \s will match a whitespace character, (i.e. space,tab,newline,carriage return).

But really you need to give more context to what you're trying to do so people can suggest more fitting solutions.

Peter Boughton