简体   繁体   English

正则表达式模式可排除字符串中的数字和特殊字符

[英]Regex pattern to exclude numbers and special characters in a string

Regex pattern to exclude numbers and special characters in a string. 正则表达式模式可排除字符串中的数字和特殊字符。
I need to exclude numbers and special characters in the below string. 我需要在下面的字符串中排除数字和特殊字符。

A service with name "11.KGGS.003022..CBCL.." is already active between Mon Apr 17 00:00:00 2017 and Thu Dec 30 00:00:00 9999. 在2017年4月17日星期一00:00:00至12月30日星期四00:00:00 9999之间,名称为“ 11.KGGS.003022..CBCL ..”的服务已处于活动状态。

i need to use the pattern in tableau and regex[^0-9] is not working in it. 我需要在Tableau中使用该模式,而regex[^0-9]在其中不起作用。 Kindly help me out. 请帮我。

you can use replaceAll like so : 您可以这样使用replaceAll

str = str.replaceAll("[0-9!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?]", "");

Output 输出量

A service with name KGGSCBCL is already active between Mon Apr    and Thu Dec  

I'm not sure if the space consider like a special character or not, if yes you can use this instead : 我不确定空格是否认为是特殊字符,如果是,则可以改用:

str = str.replaceAll("[0-9!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?\\s\\n]", "");
//------------------------------------------------Space and back line ^--^

Output 输出量

AservicewithnameKGGSCBCLisalreadynactivebetweenMonAprandThuDec

In case you want to make only one space between each word you can use this : 如果您只想在每个单词之间留一个空格,可以使用以下命令:

str = str.replaceAll("[0-9!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?]", "").
        replaceAll("\\s+", " ");
//------------------^^^^----^---------replace multiple spaces with only one

Output 输出量

A service with name KGGSCBCL is already nactive between Mon Apr and Thu Dec 

Solution 2 : 解决方案2:

exclude numbers and special characters the rest should be only the alphabetic [a-zA-Z] , so you can use also this : 排除数字和特殊字符 ,其余应仅是字母[a-zA-Z] ,因此您也可以使用以下代码:

str = str.replaceAll("[^a-zA-Z]", "");

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

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