[英]Escape path separator in a regular expression
I need to write a regular expression that finds javascript files that match我需要编写一个正则表达式来查找匹配的 javascript 文件
<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 :问题是 Windows 中的文件分隔符没有被正确转义:
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 ?有什么方法可以使用在 Windows 和 UNIX 系统中都可以使用的通用正则表达式?
Does Pattern.quote(File.separator)<\/code> do the trick?
Pattern.quote(File.separator)<\/code>能解决问题吗?
EDIT: This is available as of Java 1.5 or later.编辑:这在 Java 1.5 或更高版本中可用。 For 1.4, you need to simply escape the file separator char:对于 1.4,您需要简单地转义文件分隔符字符:
"\\" + File.separator
Is there any way to use a common regular expression that works in both Windows and UNIX systems ?有什么方法可以使用在 Windows 和 UNIX 系统中都可以使用的通用正则表达式?
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.这是安全的,因为 Windows 和 Unix 都不允许在文件或目录名称中使用这些字符。
Can't you just use a backslash to escape the path separator like so:你不能像这样使用反斜杠来转义路径分隔符:
pattern = Pattern.compile(
"^(.+?)\\" +
File.separator +
"js\\" +
File.separator +
"(.+?).js$" );
Why don't you escape File.separator
:你为什么不逃避File.separator
:
... +
"\\" + File.separator +
...
to fit Pattern.compile
requirements?适合Pattern.compile
要求? I hope "/" (unix case) is processed as a single "/".我希望将“/”(unix 大小写)作为单个“/”处理。
我已经在 Unix 系统上测试了 gimel 的答案 - 将"\\\\" + File.separator
工作正常 - 模式中生成的"\\/"
正确匹配单个"/"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.