• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

regex and backslash..

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you very much guys.I was on this problem.
 
Acetylsalicylic acid is aspirin. This could be handy too:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic