Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx for "does not begin with"

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.

like image 351
dalawh Avatar asked May 16 '13 03:05

dalawh


People also ask

Does not start with number regex?

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.

What does this regex do?

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.


1 Answers

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).

like image 193
Gordon Davisson Avatar answered Sep 22 '22 01:09

Gordon Davisson