School of Physics, Mathematics, and Computing 
Not logged in (login)
help4407

This forum is provided to promote discussion amongst students enrolled in CITS4407 Open Source Tools and Scripting.

Before posting a question here, you might want to search the forum for previous posts asking the same question.
And please keep the discussion civil (as required by UWA's Computer Use Policy, #9).
 

Options:
RSS cloud
Jump to:

^ meaning in regex

2 of 108 articles shown, currently no other people reading this forum.
photo
From: William A.
Date: Thu 15th Apr 2021, 7:27pm
Actions: 
Login-to-reply

Hi,

I'm a little confused at the meaning of the caret symbol ^ in regular expression. From 
what I understand it means the "start of a line" but I looked up negation in regular 
expression and it seems to be the same symbol. Could you please clarify this for me?

Thanks!

^ meaning in regex

photo
From: Arran S.
Date: Mon 19th Apr 2021, 1:05pm
Actions: 
Login-to-reply

"William Au" <22*5*0*[email protected]*u*e*t*u*a*e*u*a*> wrote:

> Hi,
> 
> I'm a little confused at the meaning of the caret symbol ^ in regular expression. From 
> what I understand it means the "start of a line" but I looked up negation in regular 
> expression and it seems to be the same symbol. Could you please clarify this for me?
> 
> Thanks!

Hi William,

Good question!

When found at the start of a regex, "^" does indeed match the start of a
line. So "^alpha" means that to match, a line must contain "alpha" at
the start of a line.

Okay, so what about this regex?:

alpha^beta

It is basically meaningless, because it's asking for the string "beta"
to appear at the start of a line, but ALSO for the string "alpha" to
appear before it, and it's impossible for both of those things to be true.

In an ideal world, that would be all we need to know about the caret
symbol. But the designers of grep decided (unfortunately, in my opinion)
that since the caret couldn't be validly used anywhere besides the start
of a regex, they'd put it to a double purpose.

The second meaning of the caret is: when it appears inside square
brackets, it matches the *complement* of what the square brackets would
normally match. For instance, the regex [adz] normally matches either
lowercase 'a', or lowercase 'd', or lowercase 'z'. But the regex [^adz]
matches any character *other than* a or d or z.

Both of these usages are explain in the `grep` manual (type "man grep"
to see it), which is not all that long, so I recommend you read it.
Read the sections entitled "Character Classes and Bracket Expressions"
and "Anchoring".

I hope that helps. Let me know if you've read the man page and things
are still not clear.
This Page


Program written by: [email protected]
Feedback welcome
Last modified:  6:26am Aug 12 2021