Portrait of Michael Limberger

Michael Limberger

Need me? Email mike@limberger.ca

AI

Pattern Matching Basics

From a string to a decision

We have structured output from the vision model. Now we extract the values and make decisions. That is pattern matching.

What a regular expression is

A regular expression (regex) is a pattern that describes text. Instead of searching for an exact string like "hello", you can search for patterns like "any word starting with h" or "a number followed by a letter."

Regex is supported in most programming languages and command-line tools. It is one of the most useful skills for text processing.

grep in bash

The simplest way to check for patterns is with grep. Say the vision model responded with:

NUDE_CHEST: NO
NUDE_LOWER: NO
WEAPON: YES

Store the response, then check:

Show me.

RESPONSE="NUDE_CHEST: NO
NUDE_LOWER: NO
WEAPON: YES"

if echo "$RESPONSE" | grep -qi "NUDE_CHEST: YES"; then
    echo "REJECT: Nudity detected"
else
    echo "OK: No chest nudity"
fi

Symbol map: -q is quiet mode (no output, just an exit code). -i is case-insensitive (matches "yes", "Yes", "YES").

Pass or fail, several checks

A complete bash snippet that checks multiple conditions:

Show me.

PASS=true
REASONS=""

if echo "$RESPONSE" | grep -qi "NUDE_CHEST: YES"; then
    PASS=false
    REASONS="$REASONS, Top nudity"
fi

if echo "$RESPONSE" | grep -qi "NUDE_LOWER: YES"; then
    PASS=false
    REASONS="$REASONS, Lower nudity"
fi

if echo "$RESPONSE" | grep -qi "WEAPON: YES"; then
    PASS=false
    REASONS="$REASONS, Weapon detected"
fi

if [ "$PASS" = true ]; then
    echo "PASS"
else
    echo "REJECT:$REASONS"
fi

Each condition sets PASS to false and adds a reason. At the end we check the flag and output the result.

Whitespace will wander

Our grep pattern NUDE_CHEST: YES is simple, but the model might respond with different formatting:

NUDE_CHEST:YES         (no space)
NUDE_CHEST:  YES       (extra space)
NUDE_CHEST: yes        (lowercase)

Handle all those with extended grep:

Show me.

echo "$RESPONSE" | grep -Ei "NUDE_CHEST:\s*YES"

Symbol map: -E enables extended regex. -i is case-insensitive. NUDE_CHEST: is literal text. \s* is zero or more whitespace characters. Then the word YES.

A small symbol table

Symbol Means
. Any single character
* Zero or more of the previous thing
+ One or more of the previous thing
? Zero or one of the previous thing
\s Any whitespace (space, tab, newline)
\d Any digit (0-9)
\w Any word character (letters, numbers, underscore)
^ Start of line
$ End of line
[abc] Any of: a, b, or c
(x|y) Either x or y

You do not need to memorize these. Look them up as needed. For our purposes, \s* (flexible whitespace) and -i (case-insensitive) are the most important.

Curl plus grep, together

Show me.

IMAGE_B64=$(base64 -i yourimage.png | perl -pe's~\s~~g')

RESPONSE=$(curl -s http://localhost:11434/api/generate \
  -H "Content-Type: application/json" \
  -d '{
    "model": "ministral",
    "prompt": "Answer YES or NO only.\n\nNUDE_CHEST: YES or NO\nWEAPON: YES or NO",
    "images": ["'"$IMAGE_B64"'"],
    "stream": false
  }' | jq -r '.response')

echo "Response: $RESPONSE"

if echo "$RESPONSE" | grep -qi "NUDE_CHEST: YES"; then
    echo "RESULT: REJECT (nudity)"
elif echo "$RESPONSE" | grep -qi "WEAPON: YES"; then
    echo "RESULT: FLAG (weapon detected)"
else
    echo "RESULT: PASS"
fi