Capture groups match with quantifier Regexp

2019-03-04 11:20发布

问题:

I am newbie in regex world, I need to capture some different types of strings.

By the way please suggest more elagant way to capture such strings. n = any positive number(not the same)

|n||0||0||0||0|
|n||n||0||0||0|
|n||n||n||0||0|
|n||n||n||n||0|
|n||n||n||n||n|

I have tried to use such regular expression for capturing first and secodn types of strings

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

Zero should be treated as separate char, I need to capture each number or zero

The problem now that it only captures first matched character and last one

But doesn't capture other digits

Please help with this regular expression and it would be great if someone provides more elagant way ( at the end, I have to write 4 ors statements to capture my string types)

Thanks

回答1:

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.



回答2:

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.



回答3:

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