Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

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

 
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!!
 
Sheriff
Posts: 21865
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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!
 
Die Fledermaus does not fear such a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!