in reply to Regex to match ascending sequence

What did you expect $c++ to do, knowing that the first time the code is run the value of $c is the current regular expression ?

Anyway, here is something that should do what you want:

'234565432' =~ / (\d) (?{ $c = $1}) # Match the first number and initialize $c (?: (??{ ++$c }) # Same as the previous char + 1 )*/x; print $&;

Replies are listed 'Best First'.
Re^2: Regex to match ascending sequence
by ExReg (Priest) on Oct 15, 2015 at 17:19 UTC
    Saints be praised! OK, maybe Curates be praised! It works. And it makes sense. Can't beat that. I can feel scales of ignorance falling off my eyes as I type.
Re^2: Regex to match ascending sequence
by Anonymous Monk on Oct 15, 2015 at 17:24 UTC
    Try 148012 :)
      > Try 148012 :)

      This should do

      my $regex = qr/ \d (??{ substr($&, -1) + 1 }) + /x; "148012" =~ $regex; print ">$&<" __END__ >012<

      The minimal length of the sequence can be set by {x,} instead of the + quantifier. ¹

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

      ¹) with x = min-1

        Beautiful. I substituted the {x,} in for the + as you suggested and it worked on longer sequences. These will go a long way to help me understand how to construct dynamic regexes.
      I think that it is to be expected with left-most, longest. It would find the 1, not find a 2 after it and quit, without seeing that there is a 012 later.