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) : 0

  • length(( $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

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -