3

I need to make a regex pattern to check if whole input string is made out of characters a-z, A-Z, 0-9 without spaces in the middle. I tried using ^[ ]*[a-zA-Z0-9][ ]*$ to find but it doesn't work for some reason.

For example, " dnsjkfs32NJ" is a valid string but "fds fsdfs" is not.

Thanks. :)

1
  • Where are you using this regex? Perl? PHP? Ruby? grep? C#? Commented Dec 28, 2012 at 19:11

1 Answer 1

5

This might work for you

^\s*[a-zA-Z0-9]+\s*$

You're missing a + after your character class; without it, it's matching one and only one character.

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

2 Comments

\s is any whitespace: space, tab, etc.
personally, I'd use a trim function, instead of whitespace matching. Personal preference, but I find I often need to trim the string anyway.

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.