(zsh.info.gz) The zsh/pcre Module
Info Catalog
(zsh.info.gz) The zsh/parameter Module
(zsh.info.gz) Zsh Modules
(zsh.info.gz) The zsh/regex Module
22.19 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".
Info Catalog
(zsh.info.gz) The zsh/parameter Module
(zsh.info.gz) Zsh Modules
(zsh.info.gz) The zsh/regex Module
automatically generated by
info2html