regex - Get number of trailing slashes in a string in Perl -
is there way in perl number of trailing slashes in given string, ideally in 1 line? example, if string abc\\\, return 3.
i know how on multiple lines, looking solution allow me inline, e.g. inside if-statement condition.
thanks!
some solutions:
$s =~ /(\\*)\z/, length($1) # use in scalar context.$s =~ /(\\+)\z/ ? length($1) : 0length(( $s =~ /(\\*)\z/ )[0])$s =~ s/^.*[^\\\\]//s, length($s) # destructive. use in scalar context.length( $s =~ s/^.*[^\\\\]//sr ) # requires perl 5.14+.
the last 2 should fastest since start end of string.
Comments
Post a Comment