The following checks if it begins with "End":
if [[ "$line" =~ ^End ]]
I am trying to find out how to match something that does not begin with "02/18/13". I have tried the following:
if [[ "$line" != ^02/18/13 ]]
if [[ "$line" != ^02\/18\/13 ]]
Neither of them seemed to work.
First, to negate a character class, you put the ^ inside the brackets, not before them. ^[0-9] means "any digit, at the start of the string"; [^0-9] means "anything except a digit". Second, [^0-9] will match anything that isn't a digit, not just letters and underscores.
Short for regular expression, a regex is a string of text that lets you create patterns that help match, locate, and manage text. Perl is a great example of a programming language that utilizes regular expressions. However, its only one of the many places you can find regular expressions.
bash doesn't have a "doesn't match regex" operator; you can either negate (!
) a test of the "does match regex" operator (=~
):
if [[ ! "$line" =~ ^02/18/13 ]]
or use the "doesn't match string/glob pattern" operator (!=
):
if [[ "$line" != 02/18/13* ]]
Glob patterns are just different enough from regular expressions to be confusing. In this case, the pattern is simple enough that the only difference is that globs are expected to match the entire string, and hence don't need to be anchored (in fact, it needs a wildcard to de-anchor the end of the pattern).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With