This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regular expressions to handle sequence of special characters

 
yuvaraj KumarAmudhan
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the output of the Java class I posted.
 
yuvaraj KumarAmudhan
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its working Darryl Burke. But a kind request, can you explain this -> "([\\\\/])(?=\\1)"?

Thank You!
 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic