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 n
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
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.