11

Hey all, so I'm trying to allow some text input which goes through a regex check before it's sent off. I want the text to only include A-Z, 0-9, and the space " " character. Here is my code now:

if(!title.matches("[a-zA-Z0-9_]+") {
    //fail
}
else {
    //success
}

However this still gives the //fail result when I input something like "This is a test"

Any ideas? Thanks all.

JMRboosties
  • 15,500
  • 20
  • 76
  • 116

2 Answers2

32

You're not including the space in the Regex. Try the following:

if (!title.matches("[a-zA-Z0-9 ]+"))
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
2

\s allows for any ASCII whitespace character. Consider using that rather than " ".

if(!title.matches("[a-zA-Z0-9_\s]+")) {
    //fail
}
else {
    //success
}
Scott Nguyen
  • 103
  • 8
  • 4
    No, `\s` is only for 7-bit data. It doesn’t work on Java’s native character set. – tchrist Apr 08 '11 at 23:33
  • That's news to me! I guess I should be a little more careful. – Scott Nguyen Apr 08 '11 at 23:38
  • 1
    That’s the problem with the standard Java regex library. It doesn’t work with Java’s own character set! You have to JNI over to ICU’s to get anything that works properly — by which I mean, as a bare minimum, complies with the Level 1 requirements for basic Unicode functionality which are spelled out [UTS#18 Unicode Regular Expressions](http://unicode.org/reports/tr18/). Java doesn’t meet most of those, let alone anything further up in Level 2 or Level 3 land. That’s why Google does the JNI-to-ICU thing for Android. They need Unicode support. – tchrist Apr 08 '11 at 23:51
  • I think that `\\s` will work instead of `\s`. – Phil Freihofner Oct 11 '22 at 05:08