aspose file tools*
The moose likes Java in General and the fly likes regex question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "regex question" Watch "regex question" New topic
Author

regex question

John Eric Hamacher
Ranch Hand

Joined: Apr 25, 2007
Posts: 230
Hello:

My requirement for a string of characters: May contain any letter, spaces, dashes or single quotes but you cannot have two consecutive spaces, two consecutive dashes or two consecutive single quotes.

I first tried to see if I could prevent just consecutive spaces. I figured this would be the correct regex:

^[a-zA-Z\-\s'-[\s{2}]]+$

or

^[a-zA-Z\\-\\s'^[\\s{2}]]+$

or

^[a-zA-Z\\-\\s'[^\\s{2}]]+$

but I haven't come up with the solution. What would be the correct regex?

Thanks.

Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Hi,

try this pattern:

Output from this program:


Edit:
Sorry, the above pattern matches double quotes instead of single quotes,
correct pattern should be:
s.matches("^(?!.*([\\-\',\\s])\\1)[a-zA-Z\\s\\-,\']+$")
John Eric Hamacher
Ranch Hand

Joined: Apr 25, 2007
Posts: 230
Thanks! I'm going to study it to see how it works.
John Eric Hamacher
Ranch Hand

Joined: Apr 25, 2007
Posts: 230
Wait it's allowing numbers to be present

"d - '4s" is passing though but it shouldn't
John Eric Hamacher
Ranch Hand

Joined: Apr 25, 2007
Posts: 230
Sorry, my mistake
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regex question