12

Try as I might, I can't get a RegEx to exclude space or single quotes.

  • The string "abc" is allowed
  • Not allowed: "a'bc", "'", "'abc", "'''", "abc''" etc
  • Spaces could replace the ' too in the above example
  • Trailing and leading spaces are assumed to be removed already
  • Empty strings are checked elsewhere
  • Target language is javascript

I'd use PATINDEX if I was in SQL.

Or NOT a positive match on either space or single quote, if I could negate...

I've tried (for single quote only)

  • \w*[^']\w*
  • ^\w*[^']\w*$
  • others I forget now

Please put me out of my misery so I can sleep tonight.

Edit:

  • Target string will not be surrounded by Quotes. I thought thy might add clarity
  • If "Target language is javascript" is wrong, then it's c#. I'd have to check where we do the validation exactly: client javascript or server c#
2
  • What regular expression language? Python, Perl, C#, Java? They are all subtly different. Commented Jul 14, 2009 at 20:21
  • It the test string always going to be surrounded in double quotes? Commented Jul 14, 2009 at 20:22

4 Answers 4

14

^[^\'\ ]*$
?

Sign up to request clarification or add additional context in comments.

1 Comment

Isn't it more standard to use the \s character class for the whitespace? (and at least in some flavors of regex, \S matches NON-whitespace...)
8

Quite simple. Does not allow empty strings.

^[^' ]+$

3 Comments

wuub answered first but thanks +1. This also works. Why + and not * though please?
+1 for the non escaped version. You're probably better regexper that I am :)
+ instead of * to disallow empty strings.
2

i think this

^\w*$

should work as \w does not include single quote or space.

Comments

1

Without reading the details, I don't see [^ '] in there anywhere (with a space and a single-quote).

1 Comment

Correct. I was initially testing for single quote only

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.