• 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

Regular expression help

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the reply! that will make it a lot more efficient! thanks!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic