简体   繁体   English

如何删除除两位数之间的点或逗号以外的所有非字母数字

[英]How to remove all non-alphanumeric except dot or comma between 2 digits

I've searched a long time, but unfortunately, regex are really not for me... 我已经搜索了很长时间,但是不幸的是,正则表达式真的不适合我...

I just want to replace all the non-alphanumerics except dot or comma between 2 digits (or plus) by a space. 我只想用空格替换2位数(或加号)之间的点或逗号以外的所有非字母数字。

Example : Welcome to RegExr v2.1 by gskinner.com, proudly hosted by Media Temple ! 示例: 欢迎使用由Media Temple自豪地托管的gskinner.com的RegExr v2.1! -98.7 3.141 .6180 9,000 -98.7 3.141 .6180 9,000

became 成为

Welcome to RegExr v2.1 by gskinner com proudly hosted by Media Temple 98.7 3.141 6180 9,000 欢迎使用Media Temple自豪地主持的gskinner com的RegExr v2.1 98.7 3.141 6180 9,000

I know that this can remove all non-alphanumerics : [^a-zA-Z0-9 ] but I just want to add the exception for dot or comma between 2 numbers (I've test on http://regexr.com/ ) 我知道这可以删除所有非字母数字:[^ a-zA-Z0-9],但我只想为两个数字之间的点或逗号添加例外(我已经在http://regexr.com/上进行了测试)

Thanks for your help ! 谢谢你的帮助 ! ;) ;)

Try this: 尝试这个:

str = str.replaceAll("[^a-zA-Z0-9 .,]|(?<!\\d)[.,]|[.,](?!\\d)", "");

The regex matches 正则表达式匹配

  • everything you definitely don't want, or 您绝对不想要的一切,或者
  • a dot/comma not preceded by a digit, or 带数字的点/逗号,或
  • a dot/comma not followed by a digit 点号/逗号后没有数字
[a-zA-Z ]|\d([\.,]\d)?

The above will select the text you desire to keep. 上面将选择您想要保留的文本。

It selects either Alphabetic characters + spaces and digits with optionally embedded .'s and ,'s. 它选择字母字符+空格和数字,并可选地嵌入。和。 '|' '|' works as a boolean OR. 用作布尔OR。

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

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