如何在正则表达式中转义特殊字符***(。*)

时间:2022-10-16 22:25:42

I am new to Java. Can somebody help me?

我是Java新手。有人能帮助我吗?

Is there any method available in Java which escapes the special characters in the below regex automatically?

Java中是否有任何方法可以自动转义下面正则表达式中的特殊字符?

Before escaping ***(.*) and after escaping \\*\\*\\*(.*)

在转义***(。*)之前和转义之后\\ * \\ * \\ *(。*)

I don't want to escape (.*) here.

我不想在这里逃避(。*)。

1 个解决方案

#1


6  

On the face of it, Pattern.quote appears to do the job.

从表面上看,Pattern.quote似乎可以完成这项工作。

However, looking at the detail of your question, it appears that you want / expect to be able to escape some meta-characters and not others. Pattern.quote won't do that if you apply it to a single string. Rather, it will quote each and every character. (For the record, it doesn't use backslashes. It uses "\E" and "\Q".\ which neatly avoids the cost of parsing the string to find characters that need escaping.)

但是,查看问题的详细信息,您似乎希望/期望能够逃避某些元字符,而不是其他元字符。如果将它应用于单个字符串,Pattern.quote将不会这样做。相反,它会引用每个角色。 (对于记录,它不使用反斜杠。它使用“\ E”和“\ Q”。\它可以很好地避免解析字符串以查找需要转义的字符的成本。)

But the real problem is that you haven't said how the quoter should decide which meta-characters to escape and which ones to leave intact. For instance, how does it know to escape the first three '' characters, but not the "."?

但真正的问题是你还没有说过引用者应该如何决定逃避哪些元字符以及哪些元字符完好无损。例如,它如何知道逃避前三个''字符,而不是“。”?

Without a clearer specification, your question is pretty much unanswerable. And even with a specification, there is little chance of finding an easy way to do this.

没有更清晰的规范,你的问题几乎无法回答。即使有规范,也很难找到一种简单的方法来做到这一点。

IMO, a better approach would be to do the escaping before you assemble the pattern from its component parts ... assuming that's what is going on here.

IMO,更好的方法是在从组件中组装模式之前进行转义...假设这是在这里发生的事情。

#1


6  

On the face of it, Pattern.quote appears to do the job.

从表面上看,Pattern.quote似乎可以完成这项工作。

However, looking at the detail of your question, it appears that you want / expect to be able to escape some meta-characters and not others. Pattern.quote won't do that if you apply it to a single string. Rather, it will quote each and every character. (For the record, it doesn't use backslashes. It uses "\E" and "\Q".\ which neatly avoids the cost of parsing the string to find characters that need escaping.)

但是,查看问题的详细信息,您似乎希望/期望能够逃避某些元字符,而不是其他元字符。如果将它应用于单个字符串,Pattern.quote将不会这样做。相反,它会引用每个角色。 (对于记录,它不使用反斜杠。它使用“\ E”和“\ Q”。\它可以很好地避免解析字符串以查找需要转义的字符的成本。)

But the real problem is that you haven't said how the quoter should decide which meta-characters to escape and which ones to leave intact. For instance, how does it know to escape the first three '' characters, but not the "."?

但真正的问题是你还没有说过引用者应该如何决定逃避哪些元字符以及哪些元字符完好无损。例如,它如何知道逃避前三个''字符,而不是“。”?

Without a clearer specification, your question is pretty much unanswerable. And even with a specification, there is little chance of finding an easy way to do this.

没有更清晰的规范,你的问题几乎无法回答。即使有规范,也很难找到一种简单的方法来做到这一点。

IMO, a better approach would be to do the escaping before you assemble the pattern from its component parts ... assuming that's what is going on here.

IMO,更好的方法是在从组件中组装模式之前进行转义...假设这是在这里发生的事情。