正则表达式中捕获特殊字符时出错 [英] Error in regex to catch special characters

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

问题描述

我写了一个正则表达式来捕获输入字符串中的特殊字符,但它也捕获了数字。这是正则表达式,

i wrote a regular expression to catch special characters in an input string but it catches the numbers as well. Here is the regex,

final String REGEX="[^.,%*$#@?^<!&>'|/\\\\~\\[\\]{}+-=\"]*";

我只需要抓住上面提到的字符。请帮助我。

I need to catch the above mentioned characters only. Please help me.

推荐答案

您使用未转义的连字符创建了一个范围。

You created a range with the unescaped hyphen.

错位的连字符使模式与这些字符匹配:

The misplaced hyphen makes the pattern match these characters:

在课程结束时退出连字符或位置:

Escape the hyphen or place at the end of the class:

final String REGEX="[^.,%*$#@?^<!&>'|/\\\\~\\[\\]{}+=\"-]*";
                                                      ^

这是此正则表达式演示

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

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