• 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 expressions to handle sequence of special characters

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello all,

I need to validate the text field's special characters.
Any special char which should not allowable if it comes more than once continuously.

need a regular expression for this. i tried like !(\ /{2}). but i could not get the answer.

thanks.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try !([\/])(?=\1). Include all your special characters inside the character class.

I don't do JavaScript, but I tested this in Java for two special characters \ and /.
 
yuvaraj KumarAmudhan
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no its not working Darryl Burke.
if you give // as input then it should return false and it should not accept.
if you give just / as input it should accept. but it is not.

Thank you Darryl Burke.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the output of the Java class I posted.
 
yuvaraj KumarAmudhan
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its working Darryl Burke. But a kind request, can you explain this -> "([\\\\/])(?=\\1)"?

Thank You!
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"([\\\\/])(?=\\1)" is a Java String literal. Each backslashrequires another to escape it, so the resulting String object has this content: "([\\/])(?=\1)"

The Java class Pattern also requires a backslash to escape another one, so the pattern for matching is "([\/])(?=\1)"

Of this, [\/] is a character class that matches a single occurrence of \ or /. It is enclosed in parentheses ([\/]) to create capturing group / back reference #1.

(?= introduces a non capturing positive lookahead and \1 matches the content of the back reference #1.

So effectively, this matches either of \ or / where the next character is also the same.

References: java.util.regex.Pattern javadoc and
http://www.regular-expressions.info/tutorial.html
http://www.regular-expressions.info/brackets.html

Hm, now that I think about it the regex can be shortened to ([\/])\1 or as a Java String literal "([\\\\/])\\1" -- a lookahead isn't really needed.
 
Please enjoy this holographic presentation of our apocalyptic dilemma right after this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic