RegEx for "does not start with"

The following checks if it starts with "End":

if [[ "$line" =~ ^End ]] 

I'm trying to figure out how to combine something that doesn't start with "02/18/13". I tried the following:

 if [[ "$line" != ^02/18/13 ]] if [[ "$line" != ^02\/18\/13 ]] 

None of them seemed to work.

+4
source share
3 answers

bash does not have a regexp statement; you can either cancel ( ! ) the test of the operator "does match regex" ( =~ ):

 if [[ ! "$line" =~ ^02/18/13 ]] 

or use the "does not match string / global pattern" ( != ) operator:

 if [[ "$line" != 02/18/13* ]] 

Glob patterns are simply different from regular expressions to be confusing. In this case, the pattern is simple enough that the only difference is that globs is expected to match the entire string and therefore does not need to bind (in fact, it needs a pattern to unbind the end of the pattern).

+10
source

Why not just "if not"?

 if ! [[ "$line" =~ ^02/18/13 ]] 
+4
source

Using if! will do the trick. Example: Say line = "1234" using this test in bash -

 if ! echo "$line" |grep -q "^:" > /dev/null; then echo "GOOD line does NOT begin with : "; else echo "BAD - line DOES begin with : "; fi 

He will answer: "A GOOD LINE DOES NOT start with:"

+1
source

Source: https://habr.com/ru/post/1481081/


All Articles