SH-REGEX(1) SH-REGEX(1) NAME re, match - shell script regular expression handling SYNOPSIS load regex match regex [ arg... ] ${re op arg... } DESCRIPTION Regex is a loadable module for sh(1) that provides access to regular-expression pattern matching and substitution. For details of regular expression syntax in Inferno, see regexp(6). Regex defines one builtin command, match, and one builtin substitution operator, re. Match gives a false exit status if its argument regex fails to match any arg. Re pro- vides several operations, detailed below: ${re g regexp [ arg...]} Yields a list of each arg that matches regexp. ${re v regexp [ arg...]} Yields a list of each arg that does not match regexp. ${re m regexp arg} Yields the portion of arg that matches regexp, or an empty list if there was no match. ${re M regexp arg} Yields a list consisting of the portion of arg that matches regexp, followed by list elements giving the portion of arg that matched each paren- thesized subexpression in turn. ${re mg regexp arg} Similar to re m except that it applies the match consecutively through arg, yielding a list of all the portions of arg that match regexp. If a match is made to the null string, no subsequent substi- tutions will take place. ${re s regexp subs [ arg... ]} For each arg, re s substitutes the first occur- rence of regexp (if any) by subs. If subs contains a sequence of the form \d where d is a single dec- imal digit, the dth parenthesised subexpression in regexp will be substituted in its place. \0 is substituted by the entire match. If any other character follows a backslash (\), that character Page 1 Plan 9 (printed 11/18/24) SH-REGEX(1) SH-REGEX(1) will be substituted. Arguments which contain no match to regexp will be left unchanged. ${re sg regexp subs [ arg... ]} Similar to re s except that all matches of regexp within each arg will be substituted for, rather than just the first match. Only one occurrence of the null string is substituted. EXAMPLES List all files in the current directory that end in .dis or .sbl: ls -l ${re g '\.(sbl|dis)$' *} Break string up into its constituent characters, putting the result in shell variable x: x = ${re mg '.|\n' string} Quote a string s so that it can be used as a literal regular expression without worrying about metacharacters: s = ${re sg '[*|[\\+.^$()?]' '\\\0' $s} Define a substitution function pat2regexp to convert shell- style patterns into equivalent regular expressions (e.g. ``?.sbl*'' would become ``^.\.sbl.*$''): load std subfn pat2regexp { result = '^' ^ ${re sg '\*' '.*' ${re sg '?' '.' ${re sg '[()+\\.^$|]' '\\\0' $*} } } ^ '$' } SOURCE /appl/cmd/sh/regex.b SEE ALSO regexp(6), regex(2), sh(1), string(2), sh-std(1) Page 2 Plan 9 (printed 11/18/24)