• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Use regex for this?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I'm just returning to Java & programming after a few years hiatus.

I'm trying to see if a string consists of only alphanumeric characters. Is using regular expressions for this appropriate? That is, a String like "555-aws" is bad while "3lsfs" is good.

Here's the code I'm using which doesn't seem to work for more than one character. (I know, the pattern doesn't look for digits yet.) What regular expression should I be using?

String s = "ss";
Pattern p = Pattern.compile("[a-z]");
Matcher m = p.matcher(s);
System.out.println(m.matches());

Thanks.
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Denise,

You probably want to add a * to the end of your expression, indicating that it can repeat one or more times. Also, you want to consider upper case characters, and digits. If you're allowed to have underscores, you may want to use the following meta character.
\w \\A word character: [a-zA-Z_0-9]

HTH,
M
[ September 13, 2005: Message edited by: Max Habibi ]
 
Denise Hum
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I forgot about the *.
reply
    Bookmark Topic Watch Topic
  • New Topic