简体   繁体   English

查找正则表达式以返回单个组中的所有特殊字符

[英]Finding a regex to return all special characters in a single group

I want the regex to return all special characters in a single group. 我希望正则表达式在单个组中返回所有特殊字符。 I have made the regex that would return the characters using [^a-zA_Z0-9] but this one does not return the characters not being a letter or digit in a single group. 我已经制作了使用[^a-zA_Z0-9]返回正则表达式的正则表达式,但是该正则表达式不返回单个组中不是字母或数字的字符。

For example if the string to be checked is Today is ~Friday() then I am expecting that the output would be ~() in one string so that I can replace them in one go. 例如,如果要检查的字符串是Today is ~Friday()那么我期望输出将是一个字符串中的~() ,这样我就可以一次性替换它们。

But using the regex I provided I am getting: 但是使用我提供的正则表达式我得到:

0: [14,15] ~
0: [21,22] (
0: [22,23] )

Try using replaceAll method. 尝试使用replaceAll方法。

Below you have code that find all fragments matching to normal character sequence and replaces that sequence with empty string. 在下面的代码中,找到了与常规字符序列匹配的所有片段,并将该序列替换为空字符串。

    String in = "Today is ~Friday()";
    String out = in.replaceAll("[a-zA-Z0-9 ]+", "");

    System.out.println(out);

And the result is: 结果是:

~()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM