正则表达式将字符串中的文件路径替换为Java中的其他文件路径
我有一个字符串形式的文本文件'str'正则表达式将字符串中的文件路径替换为Java中的其他文件路径,java,regex,string,replace,Java,Regex,String,Replace,我有一个字符串形式的文本文件'str' # Configuration file for the Jakarta ISAPI Redirector # The path to the ISAPI Redirector Extension, relative to the website # This must be in a virtual directory with execute privileges extension_uri=/jakarta/isapi_redirect.dll
# Configuration file for the Jakarta ISAPI Redirector
# The path to the ISAPI Redirector Extension, relative to the website
# This must be in a virtual directory with execute privileges
extension_uri=/jakarta/isapi_redirect.dll
# Full path to the log file for the ISAPI Redirector
log_file=C:\\\\Programs\\\\Org\\\\Pro\\\\Client\\\\apache-tomcat-8.0.33\\\\logs\\\\isapi_redirect.log
# Log level (debug, info, warn, error or trace)
log_level=info
# Full path to the workers.properties file
worker_file=C:\\\\Programs\\\\Org\\\\Pro\\\\Client\\\\apache-tomcat-8.0.33\\\\conf\\\\workers.properties
# Full path to the uriworkermap.properties file
worker_mount_file=C:\\\\Programs\\\\Org\\\\Pro\\\\Client\\\\apache-tomcat-8.0.33\\\\conf\\\\uriworkermap.properties
我想替换所有出现的
C:\\\\Programs\\\\Org\\\\Pro\\\\Client\\\\apache-tomcat-8.0.33
与
因此,我的最后一个'str'如下所示
# Configuration file for the Jakarta ISAPI Redirector
# The path to the ISAPI Redirector Extension, relative to the website
# This must be in a virtual directory with execute privileges
extension_uri=/jakarta/isapi_redirect.dll
# Full path to the log file for the ISAPI Redirector
log_file=C:\\\\Program Files\\\\Apache Software Foundation\\\\Tomcat 9.0\\\\logs\\\\isapi_redirect.log
# Log level (debug, info, warn, error or trace)
log_level=info
# Full path to the workers.properties file
worker_file=C:\\\\Program Files\\\\Apache Software Foundation\\\\Tomcat 9.0\\\\conf\\\\workers.properties
# Full path to the uriworkermap.properties file
worker_mount_file=C:\\\\Program Files\\\\Apache Software Foundation\\\\Tomcat 9.0\\\\conf\\\\uriworkermap.properties
基本上,我想在遇到“apache”之前替换“=”之后的filepath。这个“apache”有时也可以是“Tomcat”
我访问了stackoverflow中几乎所有的答案/解决方案,并试图对它们进行调整,但没有一个能产生预期的输出
我尝试了大约8-9个正则表达式,因为太多了,我没有发布它们。“\”是正则表达式中的一个元字符,这意味着你应该编码
“\\\”
,同时“需要在Java中转义,这样如果你想匹配“\”字符,你应该编码“\\\\”
您不需要正则表达式,只需执行str=str.replace(“C:\\\\Programs\\\\Org\\\\Pro\\\\Client\\\\apache-tomcat-8.0.33”,“C:\\\\\Program Files\\\\\apache Software Foundation\\\\\tomcat 9.0”)
您收到的不想要的结果是什么?你在用什么代码?@Titus基本上我想在“=”之后替换filepath,直到遇到“apache”,这个apache有时可能是“Tomcat”well@shash678有一个模式。LITERAL
从参数中转义所有特殊字符,它使用正则表达式,但不将参数解释为正则表达式。@SHSH678是的,replace
方法使用正则表达式引擎,但由于Pattern.LITERAL
的原因,它不“支持”正则表达式语法。
# Configuration file for the Jakarta ISAPI Redirector
# The path to the ISAPI Redirector Extension, relative to the website
# This must be in a virtual directory with execute privileges
extension_uri=/jakarta/isapi_redirect.dll
# Full path to the log file for the ISAPI Redirector
log_file=C:\\\\Program Files\\\\Apache Software Foundation\\\\Tomcat 9.0\\\\logs\\\\isapi_redirect.log
# Log level (debug, info, warn, error or trace)
log_level=info
# Full path to the workers.properties file
worker_file=C:\\\\Program Files\\\\Apache Software Foundation\\\\Tomcat 9.0\\\\conf\\\\workers.properties
# Full path to the uriworkermap.properties file
worker_mount_file=C:\\\\Program Files\\\\Apache Software Foundation\\\\Tomcat 9.0\\\\conf\\\\uriworkermap.properties