Home > Back-end >  Bash Regex Syntax For Excluding All With :space: At end of String?
Bash Regex Syntax For Excluding All With :space: At end of String?

Time:01-15

How do I get this to test true only for all numbers or characters at end of $trig_chars var except a space?

Examples: trig_chars='--'. Match: --a --ab --abc --123. But Don't match --[space] (-- followed by a space).

    local compare_scan_text=${scan_text:0:$trig_chars_len}

    if [[ "$compare_scan_text" =~ "$trig_chars"[^ ]* ]]; then # Trig char forward hit.

        local l=${#hotstring}
        local scan_text=${scan_text:$l} # Remove chars & hotscan_texts from scan_text.
        local hotstring=${hotsring:$trig_chars_len} # Remove trig chars.

        hotstrings =("$hotstring") # Update hotsrings list
    fi

CodePudding user response:

if [[ $compare_scan_text =~ ^"$trig_chars"(.*[^ ]$|$) ]] ; then

i.e. $trig_chars can be followed by anything if the last char is not a space, or it can be followed by nothing at all.

By quoting the variable, the characters are interpreted literally.

CodePudding user response:

Example Input:

compare_scan_text="--a -- why -not ---me? --ab --abc --{xyz}"
  • with GNU grep:
grep -Po '(?<=^|\s)--[^\s-] ' <<< "$compare_scan_text"
  • with POSIX awk:
awk '{for (i = 1; i <= NF; i  ) if ($i ~ /^--[^-]/) print $i}' <<< "$compare_scan_text"

outputs:

--a
--ab
--abc
--{xyz}
  •  Tags:  
  • Related