Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regex question

 
John Eric Hamacher
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • 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.

 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Sorry, my mistake
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic