This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Regular expressions to handle sequence of special characters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Regular expressions to handle sequence of special characters" Watch "Regular expressions to handle sequence of special characters" New topic
Author

Regular expressions to handle sequence of special characters

yuvaraj KumarAmudhan
Ranch Hand

Joined: Aug 22, 2009
Posts: 110
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

Joined: May 03, 2008
Posts: 4523
    
    5

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 /.


luck, db
There are no new questions, but there may be new answers.
yuvaraj KumarAmudhan
Ranch Hand

Joined: Aug 22, 2009
Posts: 110
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

Joined: May 03, 2008
Posts: 4523
    
    5

This is the output of the Java class I posted.
yuvaraj KumarAmudhan
Ranch Hand

Joined: Aug 22, 2009
Posts: 110
Its working Darryl Burke. But a kind request, can you explain this -> "([\\\\/])(?=\\1)"?

Thank You!
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

"([\\\\/])(?=\\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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regular expressions to handle sequence of special characters
 
Similar Threads
Regular expression + special characters
Regular expression for allowing space, . , @ ,_ and , -
String replace
Rejex Exception
Regular Expression