• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.util.Regex question  RSS feed

 
James Dekker
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am using JDK 1.5 on WinXP.

Here's my code:



Here's my output:



I want it to say:

matched [Doe, John] from 2 to 6.

What do I change to make it display the full last and first name (with a comma inside)?

Sincerely yours,

JD
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The expression you've written matches exactly one letter, followed by one comma, followed by one whitespace char, followed by one letter. You need to replace "one" with "one or more" using the + modifier:

"[a-zA-Z]+,\\s*[a-zA-z]+"

This also uses * to indicate "zero or more" - I thought that seemed like a safer policy in case people either omit the whitespace, or insert a line break for some reason. Of course you can remove the * if you prefer to require exactly one whitespace character; it's your choice.
[ January 09, 2007: Message edited by: Jim Yingst ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!