需要在正则表达式中转义的所有特殊字符的列表 [英] List of all special characters that need to be escaped in a regex
问题描述
我正在尝试创建一个应用程序,该应用程序将消息模板与用户尝试发送的消息相匹配.我正在使用 Java 正则表达式来匹配消息.模板/消息可能包含特殊字符.
我如何获得需要转义的特殊字符的完整列表,以便我的正则表达式在最大可能的情况下工作和匹配?
是否有通用的解决方案来转义 Java 正则表达式中的所有特殊字符?
可以看Pattern类的javadoc:http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html>
如果您想要常规字符而不是特殊含义,则需要对此处列出的任何字符进行转义.
作为一个可能更简单的解决方案,您可以将模板放在 Q 和 E 之间 - 它们之间的所有内容都被视为已转义.
I am trying to create an application that matches a message template with a message that a user is trying to send. I am using Java regex for matching the message. The template/message may contain special characters.
How would I get the complete list of special characters that need to be escaped in order for my regex to work and match in the maximum possible cases?
Is there a universal solution for escaping all special characters in Java regex?
You can look at the javadoc of the Pattern class: http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html
You need to escape any char listed there if you want the regular char and not the special meaning.
As a maybe simpler solution, you can put the template between Q and E - everything between them is considered as escaped.
这篇关于需要在正则表达式中转义的所有特殊字符的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!