From 28c1c89f194ff91ae20bb579df38540115f44041 Mon Sep 17 00:00:00 2001 From: johannst Date: Wed, 20 Jan 2021 19:15:48 +0000 Subject: deploy: 77d44129ad468581e860720ccdb7643d5bb25601 --- print.html | 55 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 10 deletions(-) (limited to 'print.html') diff --git a/print.html b/print.html index c033275..2c17771 100644 --- a/print.html +++ b/print.html @@ -287,6 +287,22 @@ echo $bar[2] # 2 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:

+ +
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

Completion functions are provided via files and need to be placed in a location @@ -473,7 +489,7 @@ command 2>&1 >file

getopts <optstring> <param> [<args>]
 
@@ -490,20 +506,39 @@ command 2>&1 >file

Example

#!/bin/bash
 function parse_args() {
-	while getopts "f:c" PARAM; do
-		case $PARAM in
-			f) echo "GOT -f $OPTARG";;
-			c) echo "GOT -c";;
-			*) echo "ERR: print usage"; exit 1;;
-		esac
-	done
-	# users responsibility to reset OPTIND
-	OPTIND=0
+    while getopts "f:c" PARAM; do
+        case $PARAM in
+            f) echo "GOT -f $OPTARG";;
+            c) echo "GOT -c";;
+            *) echo "ERR: print usage"; exit 1;;
+        esac
+    done
+    # users responsibility to reset OPTIND
+    OPTIND=0
 }
 
 parse_args -f xxx -c
 parse_args -f yyy
 
+

Regular Expressions

+

Bash supports regular expression matching with the binary operator =~. +The match results can be accessed via the $BASH_REMATCH variable:

+ +
INPUT='title foo : 1234'
+REGEX='^title (.+) : ([0-9]+)$'
+if [[ $INPUT =~ $REGEX ]]; then
+    echo "${BASH_REMATCH[0]}"    # title foo : 1234
+    echo "${BASH_REMATCH[1]}"    # foo
+    echo "${BASH_REMATCH[2]}"    # 1234
+fi
+
+
+

Caution: When specifying a regex in the [[ ]] block directly, quotes will be treated as part of the pattern. +[[ $INPUT =~ "foo" ]] will match against "foo" not foo!

+

Completion

The complete builtin is used to interact with the completion system.

complete                    # print currently installed completion handler
-- 
cgit v1.2.3