aspose file tools*
The moose likes Java in General and the fly likes detect special characters in a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "detect special characters in a string" Watch "detect special characters in a string" New topic
Author

detect special characters in a string

ravi D shankar
Ranch Hand

Joined: May 27, 2005
Posts: 41
Hi guys
I have string .I am trying to to detect whether that string has special characters or not.I have gone through the java.util.regex.* package.
Special characters can be !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
I have written the following code...

String filename = "ravi@";
if(filename.matches("\\p{Punct}"));
{
SOP("file has special characters")
}
But I am not getting the output SOP line...I am doing the right thing.How can I detect the presence of special character?...Please help me
bye
ravi shankar
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Your regex will only match if the string has only one character and this is in the {punct} set. Use

if(filename.matches(".*?\\p{Punct}.*"))

Also, you have a ';' char at the end of your 'if' that I suspect is a typo.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
ravi D shankar
Ranch Hand

Joined: May 27, 2005
Posts: 41
Hi Guys
Thanks James ..that really worked great ....I have modified it to ignore '_' in the strings.But I was wondering about the following pieces of code

String filename = "sa#1m_";
Pattern p = Pattern.compile("[\\p{Punct}&&[^_]]+");
Matcher m = p.matcher(filename);
boolean b = m.find();
//if(filename.matches(".*?[\\p{Punct}&&[^_]].*"))

if(b){SOP("String has special chars")}

I have commented an if statement .... I was wondering which is more optmized and less time consuming code .... is m.find() really better than String.matches().... does find() method terminate at the occurence of the first special char or it checks the whole string even if it has already detected a special char.And also what about the lookingAt method....I just want make sure that control get terminated at the occurence of first special char.
Thanks guys
Bye
Ravi Shankar
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
The find() method finds the first occurence that matches the associated regular expression. This can be a substring and would work with your original regex. However, as noted earlier, the matches() method matches the regex against the WHOLE String. In otherwords, the matches() method has an all-or-nothing complex whereas the find() method is satisfied with as much as it can get. Also, you can call the find() method repeatedly to find the next occurence of a match.

I am relatively new to the Java regex library so I don't know what kind of performance issues are involved with either of these methods.

Layne
[ July 15, 2005: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Originally posted by ravi D shankar:
I just want make sure that control get terminated at the occurence of first special char.


It does - it stops as soon as the truth (or not) of a regex can be established.
ravi D shankar
Ranch Hand

Joined: May 27, 2005
Posts: 41
Thanx all u guys....
Now I am in a dilemma .... actually 2 .....
My scenario is ..I have a number of files coming and falling in a directory... .number is more than 50 files every 1/2 hr....the program runs every 1/2 hr once....I have to scan the filenames for Special chars ..report messages and move the files to a error dir or a success dir....

My first dilemma is that ...I want to know whether using RE is the best when compared to a method that parses characters in a filename and matches them with a string of special chars.....Not sure which is faster ....and why it is faster?

Second one is .... As I now know find() method terminates upon first occurence of special char .... Considering the above scenario I am unsure of what method to follow.... String.matches() or find() method ..which one? ... because I believe occurence of a filename is with special chars is rare.....So which one is better ....

Help me in this regard..
Thank you
Ravi Shankar
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
I want to know whether using RE is the best when compared to a method that parses characters in a filename and matches them with a string of special chars.....Not sure which is faster ....and why it is faster?

Compared with the system call required to get the file names in the first place, finding special characters by ANY method is instantaneous. Go for clarity and maintainability of the code.
Bill
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: detect special characters in a string