tags:

views:

105

answers:

3

Hey guys! I'm new to Regular Expressions... I've been asked a regular expression that accepts Alphanumerics, a few characters more, and only ONE whitespace between words.

For example : This should match :

"Hello world"

This shouldn't :

"Hello  world"

Any ideas?

This was my expression:

[\w':''.'')''(''\[''\]''{''}''-''_']+$

I already tried the \s? (the space character once or never - right? ) but I didn't get it to work.

A: 

Using Oniguruma regex syntax, you could do something like:

^[\w\.:\(\)\[\]{}\-_](?: ?[\w\.:\(\)\[\]{}\-_])*$

Assuming that the 'other characters' are . : () [] {} - _

This regex will match a string that must begin and end with a word character or one of the other allowed characters and cannot have more than one space in a row.

If you're using the x flag (ignore whitespace in regular expression) you'll need to do this instead:

^[\w\.:\(\)\[\]{}\-_](?:\ ?[\w\.:\(\)\[\]{}\-_])*$

The only difference is the \ in front of the space.

Emily
Thanks a lot! That solves my problem!I only changed the " ?" to "\s?" and it worked:" ^(?:\s?[\w\.:\(\)\[\]{}\-_])+$ "
Johnny
I used space instead of `\s` since it didn't sound like you wanted to match tab characters, newlines, and other assorted whitespace. `\s` will match anything that's considered whitespace, while using a space character will only match a space specifically.
Emily
Emily, I think this is because of the programs I was using. I guess they interpret the expressions in a different way...
Johnny
Probably it's using the `x` flag which ignores the whitespace in the regex. I've updated the answer for use with an `x` flag as well.
Emily
Yep! That does it! Thanks a lot!Im sorry I can't rate you, it says I dont have 15 points yet.
Johnny
+1  A: 

What about:

^[\w\.:\(\)\[\]{}\-]+( [\w\.:\(\)\[\]{}\-]+)*$

Matches:

  • ^[\w\.:\(\)\[\]{}\-]+: line begins with 1 or more acceptable characters (underscore is included in \w).
  • ( [\w\.:\(\)\[\]{}\-]+): look to include a single separator character and 1 or more acceptable characters.
  • *$: repeat single separator and word 0 or more times.

Tested:

  • Hello(space)World: TRUE
  • Hello(space)(space)World: FALSE
  • Hello: TRUE
  • Hello(space): FALSE
  • Hello(tab)World: FALSE
Joel Potter
Joel, can you tell me where are you testing this?: I'm trying both on Expresso and Rad Software Regular Expression designer... different results on each program :S
Johnny
http://www.regexplanet.com/simple/index.html which I believe uses a java regex engine.
Joel Potter
This is much more efficient than Emily's regex.
Alan Moore
A: 

One other thing, is it possible to use Emily's expression and have it NOT Matching if there's a TAB ?

Johnny
I've updated my original answer to help with this.Also, in future, it's best to edit your original question rather than adding an answer if you have more information to add.
Emily
You're right, thanks! I'll keep that in mind the next time.This seems like a great place!
Johnny