This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Java in General and the fly likes Regular expression help Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Regular expression help" Watch "Regular expression help" New topic
Author

Regular expression help

Karsten Daemen
Greenhorn

Joined: Nov 19, 2009
Posts: 19
Hi guys,

I need to make a regular that checks if a name starts with a capital and containq only a maximum of 2 ' (accent) and as many white spaces as you wish. i came up with this code:




I also tried a numerous other possivbilities like

and a lot of derivatives, has anyone any ideas?

Thanks a lot in advance!


Give me golf clubs, fresh air and a beautiful partner, and you can keep the clubs and the fresh air.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

Starts with a capital: ^[A-Z].
Two quotes: ''
Any whitespace: \\s*

Just add the "any whitespace" in between and this is the regex for exactly two quotes: ^[A-Z]\\s*'\\s*'\\s*$.
Now you want a maximum of two quotes so each quote is optional. That's where ? to the rescue.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Kenny Kuchera
Greenhorn

Joined: Mar 16, 2010
Posts: 9
hey, thanks for quick reply. i forgot to say though that a-z A-Z is always allowed. now i came up with following code which works perfect for what i need it to do


the only problem with this is that there is something like \'{0,2} wich would be more efficient cause lets say that I want three ' to be allowed or two @'s then the code would get really long cause i'd always have to add '? or @? so now my question is if there is anyway to make it so i can set the limit to lets say ten ' in the entire string with \'{0,10}? Thanks a lot in advance!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16816
    
  19


First, please stick with one JR account. Using two accounts is confusing and somewhat schizophrenic.

Kenny Kuchera wrote:
the only problem with this is that there is something like \'{0,2} wich would be more efficient cause lets say that I want three ' to be allowed or two @'s then the code would get really long cause i'd always have to add '? or @? so now my question is if there is anyway to make it so i can set the limit to lets say ten ' in the entire string with \'{0,10}? Thanks a lot in advance!


First, you can attach the letters clause and quote clause together...



This will make the final a-z catch the match if the quotes don't exist... which is not relevant. But it will also allow you to do this...



as the two "?" matches are now adjacent matches.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Karsten Daemen
Greenhorn

Joined: Nov 19, 2009
Posts: 19
We are two seperate persons, we're just working on the same assignment I just introduced him to this splendid forum. Apoligies for the confusion and thank you verry much for your help!
Kenny Kuchera
Greenhorn

Joined: Mar 16, 2010
Posts: 9
Thanks a lot for the reply! that will make it a lot more efficient! thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regular expression help
 
Similar Threads
regular expression problems
regular expression.
should not contain any special characters
Regular Expression.
Regular expression help