PATTERN MATCHING BASICS
We have structured output from the vision model. Now we need to extract the values and make decisions. This is where pattern matching comes in.
What is a Regular Expression?
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's one of the most useful skills for text processing.
Pattern Matching in Bash
The simplest way to check for patterns is with grep. Let's say the vision model responded with:
NUDE_CHEST: NO
NUDE_LOWER: NO
WEAPON: YES
We can check for specific values:
# Store the response in a variable
RESPONSE="NUDE_CHEST: NO
NUDE_LOWER: NO
WEAPON: YES"
# Check if NUDE_CHEST is YES
if echo "$RESPONSE" | grep -qi "NUDE_CHEST: YES"; then
echo "REJECT: Nudity detected"
else
echo "OK: No chest nudity"
fi
The grep flags:
-q Quiet mode (no output, just exit code)
-i Case-insensitive (matches "yes", "Yes", "YES")
Building Pass/Fail Logic
Here's a complete bash snippet that checks multiple conditions:
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.
The Regex Pattern Explained
Our grep pattern "NUDE_CHEST: YES" is simple, but what if the model responds with different formatting?
NUDE_CHEST:YES (no space)
NUDE_CHEST: YES (extra space)
NUDE_CHEST: yes (lowercase)
We can handle all these variations with extended grep:
echo "$RESPONSE" | grep -Ei "NUDE_CHEST:\s*YES"
Breaking it down:
-E Enable extended regex
-i Case-insensitive matching
NUDE_CHEST: Literal text to match
\s* Zero or more whitespace characters
YES The word we're looking for
This pattern matches all the variations above.
Common Regex Symbols
. 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 don't need to memorize these. Look them up as needed. For our purposes, \s* (flexible whitespace) and /i (case-insensitive) are the most important.
Putting It Together
Full bash example with curl and pattern matching:
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