java 正则表达式中的转义路径分隔符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/242792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:35:43  来源:igfitidea点击:

Escape path separator in a regular expression

javaregexescaping

提问by Guido

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 :

例如,它应该适用于两个:

  • c:\mysite\js\common.js (Windows)
  • /var/www/mysite/js/common.js (UNIX)
  • c:\mysite\js\common.js (Windows)
  • /var/www/mysite/js/common.js (UNIX)

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 系统中都可以使用的通用正则表达式?

回答by Tomalak

Does Pattern.quote(File.separator)do the trick?

Pattern.quote(File.separator)技巧吗?

EDIT: This is available as of Java 1.5 or later. For 1.4, you need to simply escape the file separator char:

编辑:这在 Java 1.5 或更高版本中可用。对于 1.4,您需要简单地转义文件分隔符字符:

"\" + 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 Mfor pointing this out in the comments!)

转义标点符号不会破坏任何内容,但无条件转义字母或数字会将它们更改为它们的特殊含义或导致PatternSyntaxException(感谢Alan M在评论中指出这一点!)

回答by Alan Moore

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 都不允许在文件或目录名称中使用这些字符。

回答by Peter van der Heijden

Can't you just use a backslash to escape the path separator like so:

您不能像这样使用反斜杠来转义路径分隔符:

pattern = Pattern.compile(
     "^(.+?)\" + 
     File.separator +
     "js\" +
     File.separator +
     "(.+?).js$" );

回答by gimel

Why don't you escape File.separator:

你为什么不逃跑File.separator

... +
"\" + File.separator +
...

to fit Pattern.compilerequirements? I hope "/" (unix case) is processed as a single "/".

满足Pattern.compile要求?我希望“/”(unix 大小写)作为单个“/”处理。

回答by Alnitak

I've tested gimel's answer on a Unix system - putting "\\" + File.separatorworks fine - the resulting "\/"in the pattern correctly matches a single "/"

我已经在 Unix 系统上测试了 gimel 的答案 - 放置"\\" + File.separator工作正常 - 导致"\/"模式正确匹配单个"/"