STRMATCH(3)

πŸ“¦ version 93vu, in opensuse-leap 16.0

πŸ’… STRMATCH(3) β€” the strmatch page, bestie edition πŸŽ€

okay so real talk, this is the strmatch page from the Library Functions Manual, and it's all about matching shell file patterns. it's short, it's cute, it's giving "tiny function, big power" energy. let's get into it ✨


✨ NAME ✨

strmatch β€” match shell file patterns 🐚


🌸 SYNOPSIS 🌸

int strmatch(char* s, char* p)
char* submatch(char* s, char* p, int m)

like, that's it. two functions. no cap. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

okay so strmatch compares the string s with the shell pattern p and returns 1 for match and 0 otherwise. simple! icon behavior honestly πŸ‘‘

submatch is the main character here β€” it does a leading substring match of the shell pattern p with the string s. and then m is the vibe check:

  • if m is 0 β†’ the match is minimal
  • otherwise β†’ a maximal match is done ✨

what does it give you back? a pointer to the first character after the matched substring. and if there's no match at all? 0. tragic, but that's the rule 😭


🌈 the pattern cheat sheet 🌈

except for & and !, each shell pattern has an equivalent egrep(1) construct. so here's the full rundown, every single row, no skipping πŸ™ˆ:

  • * β€” egrep: .* β€” 0 or more chars
  • ? β€” egrep: . β€” any single char
  • [.] β€” egrep: [.] β€” char class
  • [!.] β€” egrep: [^.] β€” negated char class
  • *(.) β€” egrep: (.)* β€” 0 or more of
  • +(.) β€” egrep: (.)+ β€” 1 or more of
  • ?(.) β€” egrep: (.)? β€” 0 or 1 of
  • (.) β€” egrep: (.) β€” 1 of
  • @(.) β€” egrep: (.) β€” 1 of
  • a|b β€” egrep: a|b β€” a or b
  • a&b β€” no egrep equivalent β€” a and b
  • !(.) β€” no egrep equivalent β€” none of

bestie notice how & and ! are the two that don't play nice with egrep? yeah. that's the whole exception. 🫢


🧚 escaping, aka the backslash moment 🧚

\ is used to escape *, ?, (, |, &, ), [, and \ β€” but only outside of [...]. so inside a char class, backslash isn't doing that job. iykyk πŸŽ€

lowkey important: that list of escapable characters is exact β€” *, ?, (, |, &, ), [, and \. don't be out here escaping random stuff and expecting magic, okay? πŸ™ˆ

πŸ’– SEE ALSO πŸ’–

  • grep(1) β€” the whole egrep-adjacent crossover episode you didn't know you needed 🌟

πŸ’€ BUGS πŸ’€

the receipts, and they're not cute:

  • An unbalanced ) terminates the top level pattern. fr fr, one stray paren and the whole top level is done for 😭
  • Nested & and ! constructs are non-intuitive and are computationally intensive. so like... they work, but they're weird to reason about AND they eat CPU. highkey a vibe killer πŸ’€

πŸ‘‘ sign-off πŸ‘‘

and that's the whole page, bestie β€” the footer is literally just STRMATCH(3) and nothing else, no date, no version, so consider this your little goodbye kiss from the Library Functions Manual. go forth and pattern-match slay πŸ’…βœ¨πŸ¦‹

πŸ“¦ where this page comes from