Capture groups match with quantifier Regexp

后端 未结 3 738
被撕碎了的回忆
被撕碎了的回忆 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
    

提交回复
热议问题