需要在正则表达式中转义的所有特殊字符的列表 [英] List of all special characters that need to be escaped in a regex

查看:428
本文介绍了需要在正则表达式中转义的所有特殊字符的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个与消息模板匹配的应用程序,以及用户尝试发送的消息。我正在使用Java正则表达式来匹配消息。模板/消息可能包含特殊字符。

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?

是否有一个通用的解决方案来转义Java正则表达式中的所有特殊字符?

Is there a universal solution for escaping all special characters in Java regex?

推荐答案

您可以查看Pattern类的javadoc: http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html

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.

作为一个可能更简单的解决方案,你可以放置模板在\ Q和\ E之间 - 它们之间的所有内容都被视为已转义。

As a maybe simpler solution, you can put the template between \Q and \E - everything between them is considered as escaped.

这篇关于需要在正则表达式中转义的所有特殊字符的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆