Fixed word boundary matching and made wildcards possible.

This commit is contained in:
Storm Dragon
2025-10-27 19:31:02 -04:00
parent d53c44cc76
commit fbce315cf6
2 changed files with 46 additions and 5 deletions

View File

@@ -69,10 +69,48 @@ for category in "${categories[@]}"; do
# Count all occurrences of this word in the message
wordLower="${word,,}"
tempMessage="$messageLower"
while [[ "$tempMessage" =~ $wordLower ]]; do
# Parse wildcard markers to determine match type
# * prefix = allow match at end of larger word (e.g., *game matches endgame)
# * suffix = allow match at start of larger word (e.g., coffee* matches coffeehouse)
# both = allow match anywhere (e.g., *play* matches gameplay)
# none = exact word match only (e.g., tea only matches tea)
prefixWild=false
suffixWild=false
if [[ "$wordLower" == \** ]]; then
prefixWild=true
wordLower="${wordLower#\*}"
fi
if [[ "$wordLower" == *\* ]]; then
suffixWild=true
wordLower="${wordLower%\*}"
fi
# Build regex pattern based on wildcard markers
if $prefixWild && $suffixWild; then
# Match anywhere in text
pattern="$wordLower"
elif $prefixWild; then
# Match at end of word or standalone
pattern="$wordLower([[:space:][:punct:]]|$)"
elif $suffixWild; then
# Match at start of word or standalone
pattern="(^|[[:space:][:punct:]])$wordLower"
else
# Exact word match only
pattern="(^|[[:space:][:punct:]])$wordLower([[:space:][:punct:]]|$)"
fi
# Use word boundary matching based on pattern
while [[ "$tempMessage" =~ $pattern ]]; do
((wordCount++))
# Remove the matched word to find more occurrences
tempMessage="${tempMessage/$wordLower/}"
# Use the actual matched portion to avoid removing partial matches
matchedWord="${BASH_REMATCH[0]}"
tempMessage="${tempMessage/"$matchedWord"/ }"
done
done