Android / Java Regex从子字符串中删除额外的零 [英] Android/Java Regex to remove extra zeros from sub-strings

查看:90
本文介绍了Android / Java Regex从子字符串中删除额外的零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我输入以下字符串:

"2.0,3.00,-4.0,0.00,-0.00,0.03,2.01,0.001,-0.03,101"

最终输出将如下:

"2,3,-4,0,0,.03,2.01,.001,-.03,101"

将删除所有前导零和尾随零,并且正/负零都将为零。

i.e. all leading and trailing zeros will be removed and both positive/negative zeros will be simply zero.

我们可以通过首先拆分字符串并为每个部分使用Regex来实现这一点。但我的字符串大小超过10000
我们如何使用 Regex 来实现这个目标?

We can achieve this by split the string first and using Regex for each part. But my string size is more than 10000.
How can we achieve this using Regex?

编辑:

答案分析:

我有使用String 0.00,-0.00,00.00,-00.00,40.00,-40.00,4.0,-4.0,4.01,-4.01,04.01,-04.01,004.04,-004.04,0004.040测试所有答案, - 0004.040,101,.40, - 。40,0.40,-0.40并通过 WiktorStribiżew 回答了所有测试用例。(参见在这里: https://regex101.com/r/tS8hE3/9 )其他答案已通过大多数情况,但不是全部。

I have tested all answers with String "0.00,-0.00,00.00,-00.00,40.00,-40.00,4.0,-4.0,4.01,-4.01,04.01,-04.01,004.04,-004.04,0004.040,-0004.040,101,.40,-.40,0.40,-0.40" and answer from Wiktor Stribiżew passed all the test cases .(see here : https://regex101.com/r/tS8hE3/9 ) Other answers were passed on most of the cases but not all.

推荐答案

更新了测试用例答案



使用以下正则表达式:

Updated test case answer

Use the following regex:

String rx = "-?0+\\.(0)+\\b|\\.0+\\b|\\b0+(?=\\.\\d*[1-9])|\\b0+(?=[1-9]\\d*\\.)|(\\.\\d*?)0+\\b";

并替换为 $ 1 $ 2 。请参阅其他演示

正则表达式匹配几个替代品,捕获字符串的某些部分,以便以后在替换时重新插入:

The regex matches several alternatives and captures some parts of the string to later re-insert during replacement:


  • - ?0 + \。(0)+ \ b - 匹配可选的 - ,后跟一个或多个 0 s后跟一个然后捕获一个 0 但匹配一个或多个匹配项(因为(...)放在 0 上并且 + 适用于此组);最后的单词边界要求在最后一个匹配的 0 之后出现非单词字符。在替换中,我们使用 $ 1 反向引用恢复 0 。因此, -00.00 00.00 将替换为 0

  • | - 或者......

  • \ .0 + \b - 在之前有一个点跟着一个或多个零,(因为该字符串是逗号分隔的)。

  • | - 或者......

  • \ b0 +(? = \.\d * [1-9]) - 一个单词边界(字符串的开头或之后的位置,),然后是一个或多个 0 s后跟 +零个或多个数字后跟一个非0位数(所以我们删除仅包含零的整数部分中的前导零)

  • | - 或者......

  • \ b0 +(?= [1-9] \d * \。) - 一个单词边界后跟一个或多个零后跟一个之前的非0位数。(因此,我们从整数部分中删除所有前导零,不等于 0 )。

  • | - 或者......

  • (\.\\ d *?)0 + \b - 捕获 +零个或多个数字,但尽可能少,直到第一个 0 ,然后只匹配一个或多个零(直到字符串结尾或)(所以,我们得到删除小数部分中的尾随零)

  • -?0+\.(0)+\b - matching an optional - followed with one or more 0s followed with a . and then captures exactly one 0 but matching one or more occurrences (because the (...) is placed on the 0 and the + is applied to this group); the word boundary at the end requires a non-word character to appear after the last matched 0. In the replacement, we restore the 0 with $1 backreference. So, -00.00 or 00.00 will be replaced with 0.
  • | - or...
  • \.0+\b - a dot followed with one or more zeros before a , (since the string is comma-delimited).
  • | - or...
  • \b0+(?=\.\d*[1-9]) - a word boundary (start of string or a location after ,) followed with one or more 0s that are followed by . + zero or more digits followed by a non-0 digit (so we remove leading zeros in the integer part that only consists of zeros)
  • | - or...
  • \b0+(?=[1-9]\d*\.) - a word boundary followed by one or more zeros followed by a non-0 digit before a . (so, we remove all leading zeros from the integer part that is not equal to 0).
  • | - or...
  • (\.\d*?)0+\b - capturing a .+zero or more digits, but as few as possible, up to the first 0, and then just matching one or more zeros (up to the end of string or ,) (so, we get rid of trailing zeros in the decimal part)

我建议使用一个非常简单且简短的正则表达式,它可以满足您的需求:

I suggest a very simple and short regex that does what you need:

-0+\.(0)+\b|\.0+\b|\b0+(?=\.\d*[1-9])

替换为 $ 1

参见正则表达式演示。简短 IDEONE演示

String re = "-0+\\.(0)+\\b|\\.0+\\b|\\b0+(?=\\.\\d*[1-9])"; 
String str = "2.0,3.00,-4.0,0.00,-0.00,0.03,2.01,0.001,-0.03,101,0.001,-0.03";
String expected = "2,3,-4,0,0,.03,2.01,.001,-.03,101,.001,-.03"; 
System.out.println(str.replaceAll(re, "$1").equals(expected)); // TRUE

说明


  • -0 + \。(0)+ \ b - 一个减号后跟一个或多个 0 s( 0 + )后跟一个文字点( \。)后面跟着一个或多个零(并且只捕获最后一个 0 (0)+ 匹配),然后是字边界(之前的位置,在此上下文中)

  • | - 或者......

  • \.0 + \ b - 一个文字点( \。 )后跟一个或多个零后跟一个单词边界(在此上下文中之前的位置,

  • | - 或者......

  • \ b0 +(?= \。\ d * [1-9]) - 一个单词边界(在此上下文中之后的位置,)后跟一个或多个必须为的零然后是一个文字点( \。),然后是零个或多个数字,然后是1到9范围内的一个数字(所以小数部分超过 0 )。

  • -0+\.(0)+\b - a minus followed with one or more 0s (0+) followed with a literal dot (\.) followed with one or more zeros (and capturing just the last 0 matched with (0)+) followed with a word boundary (location before , in this context)
  • | - or...
  • \.0+\b - a literal dot (\.) followed with one or more zeros followed with a word boundary (location before , in this context)
  • | - or...
  • \b0+(?=\.\d*[1-9]) - a word boundary (location after , in this context) followed with one or more zeros that must be followed with a literal dot (\.), then zero or more digits and then a digit from 1 to 9 range (so that the decimal part is more than 0).

这篇关于Android / Java Regex从子字符串中删除额外的零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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