aboutsummaryrefslogtreecommitdiffhomepage
path: root/print.html
diff options
context:
space:
mode:
authorjohannst <johannst@users.noreply.github.com>2021-01-20 19:15:48 +0000
committerjohannst <johannst@users.noreply.github.com>2021-01-20 19:15:48 +0000
commit28c1c89f194ff91ae20bb579df38540115f44041 (patch)
treeb113500ec67e1fdc0eb6832453b39f745fc0b4c4 /print.html
parent4ebfabeb0a29e58e6ac06435601b81ad11c564d9 (diff)
downloadnotes-28c1c89f194ff91ae20bb579df38540115f44041.tar.gz
notes-28c1c89f194ff91ae20bb579df38540115f44041.zip
deploy: 77d44129ad468581e860720ccdb7643d5bb25601
Diffstat (limited to 'print.html')
-rw-r--r--print.html55
1 files changed, 45 insertions, 10 deletions
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
</code></pre>
+<h2><a class="header" href="#regular-expressions" id="regular-expressions">Regular Expressions</a></h2>
+<p>Zsh supports regular expression matching with the binary operator <code>=~</code>.
+The match results can be accessed via the <code>$MATCH</code> variable and
+<code>$match</code> indexed array:</p>
+<ul>
+<li><code>$MATCH</code> contains the full match</li>
+<li><code>$match[1]</code> contains match of the first capture group</li>
+</ul>
+<pre><code class="language-zsh">INPUT='title foo : 1234'
+REGEX='^title (.+) : ([0-9]+)$'
+if [[ $INPUT =~ $REGEX ]]; then
+ echo &quot;$MATCH&quot; # title foo : 1234
+ echo &quot;$match[1]&quot; # foo
+ echo &quot;$match[2]&quot; # 1234
+fi
+</code></pre>
<h2><a class="header" href="#completion" id="completion">Completion</a></h2>
<h3><a class="header" href="#installation" id="installation">Installation</a></h3>
<p>Completion functions are provided via files and need to be placed in a location
@@ -473,7 +489,7 @@ command 2&gt;&amp;1 &gt;file
<ul>
<li><code>OPTARG</code>, value of last option argument</li>
<li><code>OPTIND</code>, index of the next argument to process (user must reset)</li>
-<li><code>OPTERR</code>, display errors if set to <code>1</code> </li>
+<li><code>OPTERR</code>, display errors if set to <code>1</code></li>
</ul>
<pre><code class="language-bash">getopts &lt;optstring&gt; &lt;param&gt; [&lt;args&gt;]
</code></pre>
@@ -490,20 +506,39 @@ command 2&gt;&amp;1 &gt;file
<h3><a class="header" href="#example-1" id="example-1">Example</a></h3>
<pre><code class="language-bash">#!/bin/bash
function parse_args() {
- while getopts &quot;f:c&quot; PARAM; do
- case $PARAM in
- f) echo &quot;GOT -f $OPTARG&quot;;;
- c) echo &quot;GOT -c&quot;;;
- *) echo &quot;ERR: print usage&quot;; exit 1;;
- esac
- done
- # users responsibility to reset OPTIND
- OPTIND=0
+ while getopts &quot;f:c&quot; PARAM; do
+ case $PARAM in
+ f) echo &quot;GOT -f $OPTARG&quot;;;
+ c) echo &quot;GOT -c&quot;;;
+ *) echo &quot;ERR: print usage&quot;; exit 1;;
+ esac
+ done
+ # users responsibility to reset OPTIND
+ OPTIND=0
}
parse_args -f xxx -c
parse_args -f yyy
</code></pre>
+<h2><a class="header" href="#regular-expressions-1" id="regular-expressions-1">Regular Expressions</a></h2>
+<p>Bash supports regular expression matching with the binary operator <code>=~</code>.
+The match results can be accessed via the <code>$BASH_REMATCH</code> variable:</p>
+<ul>
+<li><code>${BASH_REMATCH[0]}</code> contains the full match</li>
+<li><code>${BASH_REMATCH[1]}</code> contains match of the first capture group</li>
+</ul>
+<pre><code class="language-bash">INPUT='title foo : 1234'
+REGEX='^title (.+) : ([0-9]+)$'
+if [[ $INPUT =~ $REGEX ]]; then
+ echo &quot;${BASH_REMATCH[0]}&quot; # title foo : 1234
+ echo &quot;${BASH_REMATCH[1]}&quot; # foo
+ echo &quot;${BASH_REMATCH[2]}&quot; # 1234
+fi
+</code></pre>
+<blockquote>
+<p><strong>Caution</strong>: When specifying a <code>regex</code> in the <code>[[ ]]</code> block directly, quotes will be treated as part of the pattern.
+<code>[[ $INPUT =~ &quot;foo&quot; ]]</code> will match against <code>&quot;foo&quot;</code> not <code>foo</code>!</p>
+</blockquote>
<h2><a class="header" href="#completion-1" id="completion-1">Completion</a></h2>
<p>The <code>complete</code> builtin is used to interact with the completion system.</p>
<pre><code class="language-bash">complete # print currently installed completion handler