Capture groups match with quantifier Regexp

后端 未结 3 737
被撕碎了的回忆
被撕碎了的回忆 2021-01-28 23:52

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

相关标签:
3条回答
  • 2021-01-28 23:56

    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
    
    0 讨论(0)
  • 2021-01-28 23:57

    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.

    0 讨论(0)
  • 2021-01-29 00:13

    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.

    0 讨论(0)
提交回复
热议问题