I need to write a regular expression that finds javascript files that match
<anypath><slash>js<slash><anything>.js
For example, it should work for both :
The problem is that the file separator in Windows is not being properly escaped :
pattern = Pattern.compile(
"^(.+?)" +
File.separator +
"js" +
File.separator +
"(.+?).js$" );
Throwing
java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence
Is there any way to use a common regular expression that works in both Windows and UNIX systems ?
The backslash \ is an escape character in Java Strings. That means backslash has a predefined meaning in Java. You have to use double backslash \\ to define a single backslash. If you want to define \w , then you must be using \\w in your regex.
pathSeparator would be ; . File. separator is either / or \ that is used to split up the path to a specific file. For example on Windows it is \ or C:\Documents\Test.
To escape a metacharacter you use the Java regular expression escape character - the backslash character. Escaping a character means preceding it with the backslash character. For instance, like this: \.
Does Pattern.quote(File.separator)
do the trick?
EDIT: This is available as of Java 1.5 or later. For 1.4, you need to simply escape the file separator char:
"\\" + File.separator
Escaping punctuation characters will not break anything, but escaping letters or numbers unconditionally will either change them to their special meaning or lead to a PatternSyntaxException. (Thanks Alan M for pointing this out in the comments!)
Is there any way to use a common regular expression that works in both Windows and UNIX systems ?
Yes, just use a regex that matches both kinds of separator.
pattern = Pattern.compile(
"^(.+?)" +
"[/\\\\]" +
"js" +
"[/\\\\]" +
"(.+?)\\.js$" );
It's safe because neither Windows nor Unix permits those characters in a file or directory name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With