Pattern Match Does Not Work in Bash Script

Pattern match does not work in bash script

The extended glob syntax you are trying to use is turned off by default; you have to enable it separately in each script where you want to use it.

shopt -s extglob

Scripts should not use ls though I imagine you were using it merely as a placeholder here.

Why RegEx Matching is NOT Working in My Bash Script?

The problem is that you're quoting the regex which takes away all the special regex powers: only quote the literal bits, particularly if they are spaces. The 2nd problem is that you're using a for loop to read the file: don't do that

while IFS= read -r CD; do
if [[ "$CD" =~ ([[:alpha:][:blank:]]*)"- "([[:digit:]]*)" - "(.*) ]]
then
echo "Found ${BASH_REMATCH[2]}"
fi
done < soundtrack.txt

BASH: Pattern match not working

You need to enable extended globbing:

shopt -s extglob

I have pattern matching problem in case command in bash

The error is most likely because you have turned on extglob option in your current shell. Because sourcing the script takes the current shell's options and extended options, it works when sourcing the script.

But when doing the ./t.sh you are launching an explicit shell which does not have the option turned on by default. Since [[ operator with == turns on extglob by default, it works for the first test but fails for the case statement. To enable the option explicitly in scripts do shopt -s extglob at the top of your script.

As you can see below the pattern works with case only if the option is enabled. Try removing -O extglob from below command and you can see it doesn't work.

bash -O extglob -c 'case apple79 in apple@(14|38|79|11)) echo ok 2;; *) ;; esac'

As far why your attempt didn't work, try adding a line shopt extglob to your t.sh and repeat your tests. You'll notice that when the script is sourced you'll see extglob on and for the executed case get extglob off

Bash regex matching not working

Don't use quotes ""

if [[ "$output" =~ ^CMD\[.*?\]$ ]]; then

The regex operator =~ expects an unquoted regular expression on its RHS and does only a sub-string match unless the anchors ^ (start of input) and $ (end of input) are also used to make it match the whole of the LHS.

Quotations "" override this behaviour and force a simple string match instead i.e. the matcher starts looking for all these characters \[.*?\] literally.

Why bash if =~ regex negate a character do not work

Your regular expression (no need to escape * inside a bracket expression)

[^*]

matches any single character that is not an *. Because regular expressions are not implicitly anchored, as long as any character in the string is not a *, the match succeeds. Anchoring it

^[^*]$

matches exactly those one-character strings that are not *. It won't match any longer string.

If you add a * after it, you get a regular expression that matches 0 or more consecutive characters that are not *. Anchoring it gives you a regular expression that matches any string (including the empty string) that consists of only non-* characters.

^[^*]*$

If you only want to match non-empty strings, makes sure the string starts with non-* character, than check that the rest (if any) are also not *.

^[^*][^*]*$

That can be shortened by using + instead of * for repetition. (* matches 0 or more; + matches 1 or more.)

^[^*]+$


Related Topics



Leave a reply



Submit