<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xml:base="https://bjorn.now/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Regex on bjorn.now</title><link>https://bjorn.now/tags/regex/</link><description>Recent content in Regex on bjorn.now</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Thu, 09 Oct 2025 11:50:33 +0200</lastBuildDate><atom:link href="https://bjorn.now/tags/regex/feed.xml" rel="self" type="application/rss+xml"/><item><title>Don't quote regex patterns in Bash [[ ]] tests, because they'll match literally</title><link>https://bjorn.now/til/2025-10-09-dont-quote-regex-patterns-in-bash-tests-because-theyll-match-literally/?utm_medium=feed</link><pubDate>Thu, 09 Oct 2025 11:50:33 +0200</pubDate><guid>https://bjorn.now/til/2025-10-09-dont-quote-regex-patterns-in-bash-tests-because-theyll-match-literally/</guid><description>Don’t quote regex patterns in Bash [[ ]] tests, because they’ll match literally.
So the below won’t match because it’s looking for a literal $ in the string, instead of matching at the end of the string:
file="https://example.com/blog-post/index.html" [[ "$file" =~ "/index.html$" ]] &amp;&amp; echo MATCH But this will:
file="https://example.com/blog-post/index.html" [[ "$file" =~ /index\.html$ ]] &amp;&amp; echo MATCH Note: [[ ]] is a Bash extension of the test command that supports regexes (among other things). The [ ] is the POSIX standard test (see man test), so [[ ]] features may not work in other shells or in limited shells like ash or sh.</description><content:encoded><![CDATA[<p>Don&rsquo;t quote regex patterns in Bash <code>[[ ]]</code> <a href="https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html#index-_005b_005b">tests</a>, because they&rsquo;ll match literally.</p>
<p>So the below won&rsquo;t match because it&rsquo;s looking for a literal <code>$</code> in the string,
instead of matching at the end of the string:</p>
<div class=" copy-enabled">
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl"><span class="nv">file</span><span class="o">=</span><span class="s2">&#34;https://example.com/blog-post/index.html&#34;</span>
</span></span><span class="line"><span class="cl"><span class="o">[[</span> <span class="s2">&#34;</span><span class="nv">$file</span><span class="s2">&#34;</span> <span class="o">=</span>~ <span class="s2">&#34;/index.html</span>$<span class="s2">&#34;</span> <span class="o">]]</span> <span class="o">&amp;&amp;</span> <span class="nb">echo</span> MATCH</span></span></code></pre></div>
</div>
<p>But this will:</p>
<div class=" copy-enabled">
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl"><span class="nv">file</span><span class="o">=</span><span class="s2">&#34;https://example.com/blog-post/index.html&#34;</span>
</span></span><span class="line"><span class="cl"><span class="o">[[</span> <span class="s2">&#34;</span><span class="nv">$file</span><span class="s2">&#34;</span> <span class="o">=</span>~ /index<span class="se">\.</span>html$ <span class="o">]]</span> <span class="o">&amp;&amp;</span> <span class="nb">echo</span> MATCH</span></span></code></pre></div>
</div>
<p><strong>Note:</strong> <code>[[ ]]</code> is a Bash extension of the test command that supports regexes (among other things).
The <code>[ ]</code> is the POSIX standard test (see <code>man test</code>),
so <code>[[ ]]</code> features may not work in other shells or in limited shells like ash or sh.</p>
<p>I learned this while removing <code>index.html</code> from file paths to bust Cloudflare&rsquo;s cache for directory listings/pretty URLs.
I then used <a href="https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html">parameter expansion</a> to replace the match with an empty string, so directory URLs are also purged:</p>
<div class=" copy-enabled">
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl"><span class="nb">echo</span> <span class="si">${</span><span class="nv">file</span><span class="p">/index.html/</span><span class="si">}</span></span></span></code></pre></div>
</div>
]]></content:encoded></item><item><title>The regex | (or) operator splits the pattern left or right unless explicitly grouped</title><link>https://bjorn.now/til/2025-10-08-regex-or-operator-splits-pattern-left-or-right-unless-constrained/?utm_medium=feed</link><pubDate>Wed, 08 Oct 2025 23:34:03 +0200</pubDate><guid>https://bjorn.now/til/2025-10-08-regex-or-operator-splits-pattern-left-or-right-unless-constrained/</guid><description>The regex | (or) operator splits the pattern left or right unless explicitly grouped. Group it using (pattern) if you need to use the match later, or as a non-capturing group (?:pattern) if you do it for clarity.
I used to think there was some magic rule about how | decided where to split, but it’s simply: characters and subpatterns concatenate into a single pattern first, then | splits the entire thing left and right unless you explicitly group it. That’s because | has the lowest precedence so all other operations happen first.
So while I used to think that prefix_cat|dog would match prefix_cat and prefix_dog, it actually matches prefix_cat and then dog.
And that exact problem in a real-world example, to find if a page is linking to another, both relative or full URL:</description><content:encoded><![CDATA[<p>The regex <code>|</code> (or) operator splits the pattern left or right unless explicitly grouped.
Group it using <code>(pattern)</code> if you need to use the match later,
or as a non-capturing group <code>(?:pattern)</code> if you do it for clarity.</p>
<p>I used to think there was some magic rule about how <code>|</code> decided where to split,
but it&rsquo;s simply: characters and subpatterns concatenate into a single pattern first,
then <code>|</code> splits the entire thing left and right unless you explicitly group it.
That&rsquo;s because <code>|</code> <a href="https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_04_08">has the lowest precedence</a> so <em>all other</em> operations happen first.</p>
<p>So while I used to think that <code>prefix_cat|dog</code> would match <code>prefix_cat</code> and <code>prefix_dog</code>,
it actually matches <code>prefix_cat</code> and then <code>dog</code>.</p>
<p>And that exact problem in a real-world example, to find if a page is linking to another, both relative or full URL:</p>
<p>The regex <code>&lt;a[^&gt;]+href=[&quot;']https://www.example.com/page|/page</code> will match <code>/page</code>,
just as in the <code>dog</code> example, it&rsquo;s splitting the whole pattern and isn&rsquo;t what I want.</p>
<p>See it in action with <a href="https://regex101.com/r/6RylLz/2">the excellent Regex101.com</a>:</p>
<figure><img src="/img/2025/10-regex-01.png"
    alt="The Regex101.com site showing the Regex in action and that it matches /page">
</figure>

<p>To make it do what I want I need to constrain it in a capturing group <code>(pattern)</code>,
and since I don&rsquo;t want to reference the group later,
I constrain it inside a non-capturing group <code>(?:pattern)</code>.</p>
<p>That gives me the pattern <code>&lt;a[^&gt;]+href=[&quot;'](?:https://www\.example\.com/page|/page)</code>,
and <a href="https://regex101.com/r/kgeGyp/3">then it matches</a> both the full URL (<a href="https://www.example.com/page">https://www.example.com/page</a>) and the relative URL (/page):</p>
<figure><img src="/img/2025/10-regex-02.png"
    alt="The Regex101.com site showing the Regex in action and that it matches both links per the pattern as I intended">
</figure>

<p>I love Regex101.com. 🙂
I have commented gnarly regexes in my code with links to it,
so colleagues can paste them there and understand what&rsquo;s going on.
The sidebar explains what each subpattern does,
and the color highlighting shows what matches what in real-time.</p>]]></content:encoded></item></channel></rss>