This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes White space regex Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "White space regex" Watch "White space regex" New topic
Author

White space regex

Rakoczi Markus
Greenhorn

Joined: Mar 13, 2012
Posts: 13
Hello

I not experienced with regex expresions, and i want to check if a textfield contains white spaces, so i read the textfield content and matches with a regex expression
for example:



is my regex expression complete enough to check for any posibile white space content?

Thanks.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Try it out and see what you get. Then try and understand why you get what you get.


~ Mansukh
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8661
    
  23

Rakoczi Markus wrote:I not experienced with regex expresions, and i want to check if a textfield contains white spaces, so i read the textfield content and matches with a regex expression

Well, first: Do you want to know if the field contains ANY whitespace or if it contains ONLY whitespace?

Because:
  • 1. You should look up the API documentation for String.matches(), and make sure that it's really what you want.
  • 2. '*' means "0 or more"; and that '0' is very important.

  • You should probably also look at the java.util.regex.Pattern and java.util.regex.Matcher classes.

    There's a fair bit to know about regexes, and you can't learn it overnight.

    Winston

    Isn't it funny how there's always time and money enough to do it WRONG?
    Articles by Winston can be found here
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 43359
        
      32
    Go through the Pattern class, or the Java Tutorials.
    Look for \s and *
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: White space regex
     
    It's not a secret anymore!