只允许使用正则表达式匹配左对齐的零。

huangapple go评论69阅读模式
英文:

Allow only left aligned zeros using regex

问题

我对使用正则表达式相对不熟悉。我有一个可以采用以下形式的序列号:VV-XXXXXX-P或VVXXXXXXP
如果使用连字符变体,那么'X'的数量可以是可变的。例如,01-162-8等效于010001628。

为了识别这两种格式,我创建了以下正则表达式:

String HYPHENS = ([0]{1}[13]{1}-[1-9]{1,6}-[0-9]{1})
String NO_HYPHENS = ([0]{1}[13]{1}[0-9]{6}[0-9]{1})

但是,NO_HYPHENS 变体的问题是它允许 0 出现在序列中的任何位置
例如:010010628 不应该是一个有效的序列,因为有一个非首位的 0。

另外,我该如何创建一个正则表达式来替换序列中除第一个以外的所有 0?我尝试了以下方式,但它也会替换第一个 0。

String code = 010001234;
code = code.replaceAll("0+", "");

我该如何修改这些正则表达式以实现这一目标?

英文:

I am fairly new to using regex. I have a serial number which can take the following forms: VV-XXXXXX-P or VVXXXXXXP
If the hyphen variant is used, then the number of 'X' can be variable. For example 01-162-8 is equivalent to 010001628.

In order to identify the 2 formats, I have created the following regex's:

String HYPHENS = ([0]{1}[13]{1}-[1-9]{1,6}-[0-9]{1})
String NO_HYPHENS = ([0]{1}[13]{1}[0-9]{6}[0-9]{1})

However the issue with the NO_HYPHENS variant is that it allows 0 anywhere in the sequence
For example: 010010628 should not be a valid sequence because there's a non leading 0.

Additionally, how would I create a regex that I can use to replace all 0 from the sequence but the first one? I have tried the following but it also replaces the first 0.

String code = 010001234;
code = code.replaceAll("0+", "");

How could I modify the regex's to achieve this?

答案1

得分: 2

你可以使用:

String NO_HYPHENS = "0[13](?!0*[1-9]0)[0-9]{6}[0-9]";
code = code.replaceAll("(?!^)0(?!$)", "");

查看正则表达式演示。正则表达式0[13](?!0*[1-9]0)[0-9]{6}[0-9]匹配:

  • 0 - 零
  • [13] - 一或三
  • (?!0*[1-9]0) - 不允许在此位置出现任何后面跟着非零数字再跟零的零
  • [0-9]{6} - 六位数字
  • [0-9] - 一位数字。

我明白你在Java中使用它与.matches,否则,在开头添加^并在结尾添加$

另外,正则表达式(?!^)0(?!$)会匹配任何不在字符串开头/结尾位置的零。

英文:

You can use

String NO_HYPHENS = "0[13](?!0*[1-9]0)[0-9]{6}[0-9]";
code = code.replaceAll("(?!^)0(?!$)", "");

See the regex demo.
The 0[13](?!0*[1-9]0)[0-9]{6}[0-9] regex matches:

  • 0 - zero
  • [13] - one or three
  • (?!0*[1-9]0) - any zeros followed with a non-zero digit and then a zero is not allowed at this location
  • [0-9]{6} - six digits
  • [0-9] - a digit.

I understand you use it in Java with .matches, else, add ^ at the start and $ at the end.

Also, the (?!^)0(?!$) regex will match any zero that is not at the string start/end position.

答案2

得分: 1

^0[13]0*[1-9]*[0-9]$

^ - 字符串开始位置
0 - 第一个字符必须为零
[13] - 第二个字符必须为一或三
0* - 零的可变长度序列
[1-9] - 非零数字的可变长度序列
[0-9] - 最后一个数字(也可以用 \d 替代)
$ - 字符串结束位置

这个正则表达式有一个问题:它没有检查序列号的 XXXXXX 部分有多少个数字。但您可以使用长度函数来检查:

      String code = "010000230";
      if (code.matches("^0[13]0*[1-9]*[0-9]$") && code.length() == 9) {
         // 代码有效
      }
      // 替换
      code = code.replaceAll("^(0[13])0*([1-9]*[0-9])$", "$1$2");

替换的解释:
(0[13]) 第一组(括号中的组)
0* 一些零
([1-9]*[0-9]) 第二组

将被替换为:
$1$2 第一组和第二组($1 表示第一组)

英文:
^0[13]0*[1-9]*[0-9]$

^ - beginning of string<br>
0 - first sign must be zero<br>
[13] - second sign must be one or three<br>
0* - sequence of zeros of variable length<br>
[1-9] - sequence of non-zeros of variable length<br>
[0-9] - finally one digit (it can be replaced with \d also)<br>
$ - end of string

This regex has one problem: it doesn't check how many digits are in the XXXXXX section of serial number. But you can check it with length function:

      String code = &quot;010000230&quot;;
      if (code.matches(&quot;^0[13]0*[1-9]*[0-9]$&quot;) &amp;&amp; code.length() == 9) {
         // code is valid
      }
      // replacement
      code = code.replaceAll(&quot;^(0[13])0*([1-9]*[0-9])$&quot;, &quot;$1$2&quot;);

Explanation of the replacement:<br>
(0[13]) group number 1 (groups are in bracket)<br>
0* some zeros<br>
([1-9]*[0-9]) group number 2

This will be replaced with:<br>
$1$2 group number 1 and group number 2 ($1 means group number 1)

huangapple
  • 本文由 发表于 2020年10月16日 21:13:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64389891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定