• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reg ex help desired; should be simple, but...  RSS feed

 
Chris Simons
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess regular expressions are just not my thing.

To give some quick background, I am writing an installer using IzPack (great open-source product). Our requirements for the installer entail making sure that some of the paths/directories selected by the user for various inputs do not have spaces (but will accept all other normal path characters). I can accomplish this using IzPack's built-in regular expression validator, but I can't seem to nail the reg ex. I've been trying online resources and my own Java program to test, fix, test again, etc...

My understanding of regular expressions is not great; I understand the basics (start and ending of pattern, etc.). I know that /s should be used as the pattern for finding whitespace.

For anyone willing to spend a few minutes to show me where I have gone wrong, again, I am trying to determine whether or not the test string matches/works with a regular expression that will allow basically all characters *minus* whitespace. This is solely to avoid a potential issue with Windows in which the installed application basically hates any space in the directory/path (pretty common for older Windows apps).

Here are some examples of what I have tried for the reg ex:

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

^[a-zA-Z0-9\w]*\s$ Comment: \w should allow all characters, no?

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

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


And here are some test strings that should or should not work:

Good: /home/username
Good: C:\Users\username
Bad: C:\Program Files\My Program\
Good: /usr/jdk1.6.20/
Bad: C:\Program Files\JavaSoft\Java 1.6\

Thank you!!
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about ^[^\s]+$ or its simpler form ^\S+$ ? [^\s] means anything but \s so anything but whitespace. That one is also known as \S.
 
Chris Simons
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh my...that works perfectly. I must remember to Keep It Simple (Stupid).

Thank you!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!