[英]Remove trailing special characters with regex
I need to remove trailing decimal/dot from a statement. 我需要从语句中删除尾随的十进制/点。 Following is the statement
以下是声明
ABC paid 10.25 RS confirmed. from bank XYZ 20125722. on 23-12-2012
I want to remove the trailing decimals/dot from the statement. 我想从语句中删除尾随的小数点/点。 After removed the decimal/dot, it should be like
删除小数点后,应该是
ABC paid 10.25 RS confirmed from bank XYZ 20125722 on 23-12-2012
Please note, I don't need to remove the decimal in the 10.25
, but in other places. 请注意,我不需要删除
10.25
中的小数,而是在其他地方。 How can I do it with Java? 我该如何使用Java?
在Java中,只需对字符串使用replaceAll(String pattern,String replacement)方法,如下所示:
String result = "ABC paid 10.25 RS confirmed. from bank XYZ 20125722. on 23-12-2012".replaceAll("\\.\\s+"," ") ;
The regex you could use is /\\.\\S+/
in Perl or Perl-regex supporting languages. 您可以使用的正则表达式是Perl或Perl-regex支持语言的
/\\.\\S+/
。 Otherwise /\\.[ ^I]/
where "^I" is TAB character. 否则
/\\.[ ^I]/
,其中“ ^ I”是TAB字符。 Replace that with empty. 将其替换为空。
Hope that helps. 希望能有所帮助。
You should use is \\\\.\\\\s+ , because after the dot may contain many spaces or one space . 您应该使用\\\\。\\\\ s + ,因为点后可能包含许多空格或一个空格。
The code following: 以下代码:
String result = "ABC paid 10.25 RS confirmed. from bank XYZ 20125722. on 23-12-2012"
.replaceAll("\\.\\s+", " ");
So, you want to match any .
因此,您想匹配任何一个
.
not in a number? 没有数量? You can use a method called negative lookahead :
您可以使用一种称为负先行的方法:
"\\.(?![0-9])"
This match any .
这个匹配任何
.
NOT before a digit. 不能在数字前。
Your regex is 您的正则表达式是
\.(?=\s|$)
means match a dot when it is followed by a whitespace or the end of the string. 表示在点后跟空格或字符串末尾时匹配点。
(?=\\s|$)
is a positive lookahead that ensures that there is a whitespace or the end of the string following, but does not match this, so no need to insert it again, just replace with the empty string. (?=\\s|$)
是一个正向超前查询 ,可确保后面有空格或字符串的结尾,但不匹配此字符,因此无需再次插入,只需替换为空字符串即可。
See it here on Regexr 在Regexr上查看
For your java 对于你的java
String res = "ABC paid 10.25 RS confirmed. from bank XYZ 20125722. on 23-12-2012".replaceAll("\\.(?=\\s|$)", "");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.