简体   繁体   English

使用字符串的Pattern.compile()

[英]Pattern.compile() using a string

Consider this code: 考虑以下代码:

String path = "c:\\temp";
Pattern myPattern = Pattern.compile(".*filename.XLS.*.\\d{8}_\\d{6}");

How can I replace the first ".*" in myPattern so that path is included in the Regex ? 如何替换myPattern中的第一个“。*”,以便将路径包括在Regex中? Something along the lines of 遵循以下原则

Pattern myPattern = Pattern.compile(path + "filename.XLS.*.\\d{8}_\\d{6}");

which obviously does not work .. :) 这显然不起作用.. :)

Cheers, Tim 蒂姆,干杯

You have to escape the backslashes twice: 您必须两次转义反斜杠:

String path = "c:\\\\temp";
Pattern myPattern = Pattern.compile(path + "filename.XLS.*.\\d{8}_\\d{6}");

Once for java and than again for the regular expression. 对于Java,一次用于正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM