aspose file tools
The moose likes Java in General and the fly likes regex and backslash.. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "regex and backslash.." Watch "regex and backslash.." New topic
Author

regex and backslash..

pradeep selvaraj
Ranch Hand

Joined: Nov 29, 2005
Posts: 62
Hi,

I have a regex pattern matching code like this.

/****************CODE***********************************************
public static boolean isValidProjectFile(String fileName)
{
Pattern p = Pattern.compile("([a-z,A-Z,0-9,_, ,/,\\,-,.])+[.]+ jar|class|txt)",Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(fileName);
return m.matches();
}
/******************************************************************

When i call this method as shown below

boolean test = isValidProjectFile("test\testdata\hibernate3.jar");

It returns false. I am expecting it to return true.

Where am i going wrong?

Thanks
Pradeep


[<a href="http://cyvis.sourceforge.net" target="_blank" rel="nofollow">CyVis - Software Complexity Visualiser</a>] [<a href="http://pradeepselvaraj.blogspot.com" target="_blank" rel="nofollow">Pradeep's Blog</a>]
Ajay A Patil
Greenhorn

Joined: Apr 13, 2006
Posts: 22
Try,

Pattern p = Pattern.compile("([a-z,A-Z,0-9,_, ,/,\\\,-,.])+[.]+ jar|class|txt)",Pattern.CASE_INSENSITIVE);

In Java regular expressions, when you have to use DOUBLE BACKSLASH
as escape character. Java eats up one BACKSLASH, and the regular
expression engine eats the other BACKSLASH.
Ajay A Patil
Greenhorn

Joined: Apr 13, 2006
Posts: 22
Actually, you will also have to escape the '.' character.

Pattern p = Pattern.compile("([a-z,A-Z,0-9,_, ,/,\\\,-,.])+[\\.]+ jar|class|txt)",Pattern.CASE_INSENSITIVE);
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
In fact, you need four backslashes in the regex string to match one in the target string. And as for the dot, if you put it in a character class, it just matches a dot. So you can either put brackets around it, or put a backslash in front of it, but doing both is overkill (although it still works). The comma also has no special meaning, Pradeep; your character class will match a comma as well as all those other characters. And you don't need the parentheses around the character class, but you do need a matched pair of them around the final alternation (I'm sure that's just a typo).
(\w is the same as [A-Za-z0-9_])
pradeep selvaraj
Ranch Hand

Joined: Nov 29, 2005
Posts: 62
Thanks you very much guys.I was on this problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regex and backslash..
 
Similar Threads
Regex for capital letters
How to write a regex to include a text but exclude another text in the one regex?
Regular expressions: Grouping
Needed help in composing an regular expression
Check if a String has alphabets