Shellish(3pm)

πŸ“¦
version 0.93 Β· 6 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Regexp::Shellish β€” Shell-like regular expressions πŸ’–

okay so bestie, this is the page known as Shellish(3pm), filed under User Contributed Perl Documentation, and it's literally about giving you shell-style wildcards inside Perl regular expressions. let's get into it 🫢


✨ SYNOPSIS ✨

use Regexp::Shellish qw( :all ) ;

$re = compile_shellish( 'a/c*d' ) ;

## This next one's like 'a*d' except that it'll
## match 'a/d'.
$re = compile_shellish( 'a**d' ) ;

## And here '**' won't match 'a/d', but behaves
## like 'a*d', except for the possibility of high
## cpu time consumption.
$re = compile_shellish( 'a**d', { star_star => 0 } ) ;

## The next two result in identical $re1 and $re2.
## The second is a noop so that Regexp references can
## be easily accomodated.
$re1 = compile_shellish( 'a{b,c}d' ) ;
$re2 = compile_shellish( qr/\A(?:a(?:b|c)d)\Z/ ) ;

@matches = shellish_glob( $re, @possibilities ) ;

🌸 DESCRIPTION 🌸

so this module hands you shell-like regular expressions. the wildcards you get are "?", "*" and "**", and "**" is like "*" but β€” plot twist β€” it also matches "/". the deets live under "compile_shellish".

case sensitivity, plus constructs like <**>, "(a*b)", and "{a,b,c}", can all be disabled. yes, turned off entirely. icons only πŸŽ€

πŸ’… compile_shellish

compiles a string that holds a 'shellish' regular expression and gives you back a Regexp reference. and if you hand it a Regexp reference? it just passes it right through, unmolested, no notes. πŸ’…

and here are the transformation rules, shellish on the left, Perl RE on the right, no cap:

  • * β†’ [^/]*
  • ? β†’ .
  • ** β†’ .* β€” unless { star_star => 0 }
  • ... β†’ .* β€” unless { dot_dot_dot => 0 }
  • ( β†’ ( β€” unless { parens => 0 }
  • ) β†’ ) β€” unless { parens => 0 }
  • {a,b,c} β†’ (?:a|b|c) β€” unless { braces => 0 }
  • \a β†’ a β€” these are de-escaped and passed to quotemeta()
  • \* β†’ \* β€” same deal, de-escaped then handed to quotemeta()

the wildcards treat newlines as just normal characters, fr fr. nothing special happening there.

parens group into $1..$n, since they get passed through unmolested (unless you passed the option parens => 0). ngl though, that's useless when you're using glob_shellish().

the final parameter can be a hash reference holding options, like this:

compile_shellish(
   '**',
   {
      anchors        => 0,   ## Doesn't put ^ and $ around the
                             ## resulting regexp
      case_sensitive => 0,   ## Make case insensitive
      dot_dot_dot    => 0,   ## '...' is now just three '.' chars
      star_star      => 0,   ## '**' is now two '*' wildcards
      parens         => 0,   ## '(', ')' are now regular chars
      braces         => 0,   ## '{', '}' are now regular chars
   }
) ;

and here's each one spelled out, one at a time, because we are thorough in this house:

  • anchors => 0 β€” doesn't put ^ and $ around the resulting regexp
  • case_sensitive => 0 β€” makes it case insensitive
  • dot_dot_dot => 0 β€” '...' is now just three '.' chars
  • star_star => 0 β€” '**' is now two '*' wildcards
  • parens => 0 β€” '(' and ')' are now regular chars
  • braces => 0 β€” '{' and '}' are now regular chars
⚠️ important bestie: no option affects Regexps passed through. like, at all. none of them. periodt.

πŸ¦‹ shellish_glob

you pass in a regular expression plus a list of possible values, and you get back a list of the values that matched. that's it, that's the whole vibe:

my @matches = shellish_glob( '*/*', @possibilities ) ;
my @matches = shellish_glob( '*/*', @possibilities, \%options ) ;

πŸ‘‘ AUTHOR πŸ‘‘

Barrie Slaymaker <barries@slaysys.com> β€” absolute icon, we stan 🫢


okay and for the lil' sign-off, this whole thing is perl v5.36.0, dated 2022-10-13 πŸ’– happy globbing, bestie, go match some stuff βœ¨πŸŒˆπŸ¦‹

πŸ“¦ where this page comes from