Search code examples
regexpcre

Validate string # followed by digits but # increases after every occurance


I have a string looks like this

#123##1234###2356####69

It starts with # and followed by any digits, every time the # appears, the number of # increases, first time 1, second time 2, etc.

It's similar to this regex, but since I don't know how long this pattern goes, so it's not very useful.

^#\d+##\d+###\d+$

I'm using PCRE regex engine, it allows recursion (?R) and conditions (?(1)...) etc.

Is there a regex to validate this pattern?

Valid

  • #123
  • #12##235
  • #1234##12###368
  • #1234##12###368####22235#####723356

Invalid

  • ##123
  • #123###456
  • #123##456##789

I tried ^(?(1)(?|(#\1)|(#))\d+)+$ but it doesn't seem to work at all


Solution

  • You can do this using PCRE conditional sub-pattern matching:

    ^(?:((?(1)\1)#)\d+)++$
    

    RegEx Demo

    RegEx Details:

    • ^: Start
    • (?:: Start non-capture group
      • (: Start capture group #1
        • (?(1)\1): if/then/else directive that means match back-reference \1 only if 1st capture group is available otherwise match null
        • #: Match an additional #
      • ): End capture group #1
      • \d+: Match 1+ digits
    • )++: End non-capture group. Match 1+ of this non-capture group.
    • $: End