Capture groups match with quantifier Regexp

假如想象 提交于 2019-12-02 09:30:23

I'm not sure wheter it is sufficient for you or not:

\|(?:(0)|([0-9]+))\|

https://regex101.com/r/fX5xI4/2

Now u have to split your matches into groups of x elements where x is number of colums. I suppose that should be just fine.

How about:

^(?:\|[1-9][0-9]*\|){1,5}(?:\|0\|){0,4}$

Explanation:

^               : start of line
  (?:           : non capture group
   \|           : a pipe character
   [1-9][0-9]*  : a positive number of any length
   \|           : a pipe character
  ){1,5}        : the group is repeated 1 to 5 times
  (?:           : non capture group
    \|0\|       : a zero with pipe arround it
  ){0,4}        : group is repeated 0 to 4 times.
$               : end of line

This will match all examples you've given, ie. some positive numbers followed by zeros.

You could validate the line first, then just findall with \d+

Validate: '~^\|[1-9]\d*\|(?:\|(?:[1-9]\d*|0+(?!\|\|[1-9]))\|){4}$~'

 ^                             # BOS
 \|
 [1-9] \d*                     # Any numbers that start with non-zero
 \|

 (?:
      \|
      (?:
           [1-9] \d*                     # Any numbers that start with non-zero
        |                              # or,
           0+                            # Any numbers with all zeros
           (?! \|\| [1-9] )              # Not followed by a non-zero
      )
      \|
 ){4}
 $                             # EOS
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!