• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular expression help  RSS feed

 
Karsten Daemen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kenny Kuchera
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Karsten Daemen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the reply! that will make it a lot more efficient! thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!