From 836b1874369fa47f37a71f2f04af18e7929567ce Mon Sep 17 00:00:00 2001 From: johannst Date: Wed, 20 Jan 2021 20:11:01 +0100 Subject: bash/zsh: add notes for regex matching --- src/tools/zsh.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/tools/zsh.md') diff --git a/src/tools/zsh.md b/src/tools/zsh.md index 4d52ae0..e9aa8bf 100644 --- a/src/tools/zsh.md +++ b/src/tools/zsh.md @@ -162,6 +162,24 @@ echo ${(L)foo} # aabb echo ${(U)foo} # AABB ``` +## Regular Expressions + +Zsh supports regular expression matching with the binary operator `=~`. +The match results can be accessed via the `$MATCH` variable and +`$match` indexed array: +- `$MATCH` contains the full match +- `$match[1]` contains match of the first capture group + +```zsh +INPUT='title foo : 1234' +REGEX='^title (.+) : ([0-9]+)$' +if [[ $INPUT =~ $REGEX ]]; then + echo "$MATCH" # title foo : 1234 + echo "$match[1]" # foo + echo "$match[2]" # 1234 +fi +``` + ## Completion ### Installation -- cgit v1.2.3