in reply to opposite of index+rindex? How-to? Needed?

... what is the index of the first non-space character?

c:\@Work\Perl\monks>perl -wMstrict -le "my $s = ' a '; ;; $s =~ m{ \S }xms; print $-[0]; " 4

... as long as it didn't invoke the regex engine.

What's the point of that?

Update: More generally:

c:\@Work\Perl\monks>perl -wMstrict -le "my $s = 'xyzzyBARxkcdFOOyz'; ;; my $vowel = qr{ [AEIOUaeiou] }xms; ;; print qq{offset of first vowel: }, $s =~ m{ ($vowel) }xms ? $-[1] : ' +none'; ;; print qq{offset of last vowel: }, $s =~ m{ .* ($vowel) }xms ? $-[1] : + 'none'; ;; print qq{offset of first vowel: }, 'XYZ' =~ m{ ($vowel) }xms ? $-[1] +: 'none'; " offset of first vowel: 6 offset of last vowel: 14 offset of first vowel: none
See  @- @+ in perlvar.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: opposite of index+rindex? How-to? Needed? (updated)
by rsFalse (Chaplain) on Sep 02, 2019 at 00:26 UTC
    And alternatively with look-ahead:
    ;; print qq{offset of first vowel: }, $s =~ m{ (?= $vowel ) }xgms ? pos +$s : 'none'; ;; print qq{offset of last vowel: }, $s =~ m{ .* (?= $vowel ) }xgms ? po +s $s : 'none';
    Note 'g' modifier.

    To negate, one can use negative look-ahead (?! $vowel ). Upd. Not really! Look at the AnomalousMonk reply.
      ... with look-ahead ...

      $+[0] also works here (note  $+[0] vice  $-[1] and no  /g modifier):

      c:\@Work\Perl\monks>perl -wMstrict -le "my $vowel = qr{ [AEIOUaeiou] }xms; ;; my $s = 'xyzzyBARxkcdFOOyz'; ;; print 'offset of first vowel: ', $s =~ m{ (?= $vowel) }xms ? $+[0] : +'none'; print 'offset of last vowel: ', $s =~ m{ .* (?= $vowel) }xms ? $+[0] +: 'none'; print 'offset of first vowel: ', 'XYZ' =~ m{ (?= $vowel) }xms ? $+[0] + : 'none'; " offset of first vowel: 6 offset of last vowel: 14 offset of first vowel: none
      I like the look-ahead idea because it avoids a capture and so may be slightly faster.

      To negate, one can use negative look-ahead (?! $vowel ).

      I think there's a problem here:

      c:\@Work\Perl\monks>perl -wMstrict -le "my $vowel = qr{ [AEIOUaeiou] }xms; ;; my $s = 'aePDQioVWXua'; ;; print 'offset of first non-vowel: ', $s =~ m{ (?! $vowel) }xmsg ? pos +($s) : 'none'; print 'offset of last non-vowel: ', $s =~ m{ .* (?! $vowel) }xmsg ? p +os($s) : 'none'; ;; $s = 'aei'; print 'offset of first non-vowel: ', $s =~ m{ (?! $vowel) }xmsg ? pos +($s) : 'none'; " offset of first non-vowel: 2 offset of last non-vowel: 12 offset of first non-vowel: 3
      The second and third cases give questionable results because there's always a place in a string where a negative look-ahead will succeed if it is true nowhere else: just beyond the end of the string. ($+[0] has the same problem in these cases as pos.)

      Update: However, a positive look-ahead to a negated char class works:

      c:\@Work\Perl\monks>perl -wMstrict -le "my $non_vowel = qr{ [^AEIOUaeiou] }xms; ;; my $s = 'aePDQioVWXua'; ;; print 'offset of first non-vowel: ', $s =~ m{ (?= $non_vowel) }xmsg ? + pos($s) : 'none'; print 'offset of last non-vowel: ', $s =~ m{ .* (?= $non_vowel) }xmsg + ? pos($s) : 'none'; ;; $s = 'aei'; print 'offset of first non-vowel: ', $s =~ m{ (?= $non_vowel) }xmsg ? + pos($s) : 'none'; " offset of first non-vowel: 2 offset of last non-vowel: 9 offset of first non-vowel: none
      The positive assertion requires that something be present, so the overall match can fail. ($+[0] works as well as pos.)


      Give a man a fish:  <%-{-{-{-<