The zsh/pcre Module (zsh)

### 22.21 The zsh/pcre Module ¶

The `zsh/pcre` module makes some commands available as builtins:

`pcre_compile` [ `-aimxs` ] PCRE ¶

  Compiles a perl-compatible regular expression.

  Option `-a` will force the pattern to be anchored. Option `-i` will compile a case-insensitive pattern. Option `-m` will compile a multi-line pattern; that is, `^` and `$` will match newlines within the pattern. Option `-x` will compile an extended pattern, wherein whitespace and `#` comments are ignored. Option `-s` makes the dot metacharacter match all characters, including those that indicate newline.

`pcre_study` ¶

  Studies the previously-compiled PCRE which may result in faster matching.

`pcre_match` [ `-v` var ] [ `-a` arr ] [ `-n` offset ] [ `-b` ] string ¶

  Returns successfully if `string` matches the previously-compiled PCRE.

  Upon successful match, if the expression captures substrings within parentheses, `pcre_match` will set the array `match` to those substrings, unless the `-a` option is given, in which case it will set the array arr. Similarly, the variable `MATCH` will be set to the entire matched portion of the string, unless the `-v` option is given, in which case the variable var will be set. No variables are altered if there is no successful match. A `-n` option starts searching for a match from the byte offset position in string. If the `-b` option is given, the variable `ZPCRE_OP` will be set to an offset pair string, representing the byte offset positions of the entire matched portion within the string. For example, a `ZPCRE_OP` set to "32 45" indicates that the matched portion began on byte offset 32 and ended on byte offset 44. Here, byte offset position 45 is the position directly after the matched portion. Keep in mind that the byte position isn’t necessarily the same as the character position when UTF-8 characters are involved. Consequently, the byte offset positions are only to be relied on in the context of using them for subsequent searches on string, using an offset position as an argument to the `-n` option. This is mostly used to implement the "find all non-overlapping matches" functionality.

  A simple example of "find all non-overlapping matches":

      string="The following zip codes: 78884 90210 99513"
      pcre_compile -m "\d{5}"
      accum=()
      pcre_match -b -- $string
      while [[ $? -eq 0 ]] do
          b=($=ZPCRE_OP)
          accum+=$MATCH
          pcre_match -b -n $b[2] -- $string
      done
      print -l $accum

The `zsh/pcre` module makes available the following test condition:

expr `-pcre-match` pcre ¶

  Matches a string against a perl-compatible regular expression.

  For example,

      [[ "$text" -pcre-match ^d+$ ]] &&
      print text variable contains only "d's".

  If the `REMATCH_PCRE` option is set, the `=~` operator is equivalent to `-pcre-match`, and the `NO_CASE_MATCH` option may be used. Note that `NO_CASE_MATCH` never applies to the `pcre_match` builtin, instead use the `-i` switch of `pcre_compile`.