• 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

regex question

 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! I'm going to study it to see how it works.
 
John Eric Hamacher
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wait it's allowing numbers to be present

"d - '4s" is passing though but it shouldn't
 
John Eric Hamacher
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, my mistake
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic