Regex: What does the ^ do? What is it called?

A

Al Cholic

Hello,

Im trying to understand what the ^ and $ characters do when used in
regular expressions.

For example:

/^abc/ =~ "!abc"
returns nil
"You can use an anchor to match the beginning of a string"


/abc$/ =~ "abc!"
returns nil
"You can use an anchor to match the end of a string"


Why do these two statements return nil and how does the "anchor" work
exactly?

Can someone please clarify?


Does this mean that the regex is checking before the C\d+ ?
if @refdes[0] =~ /^C\d+/
return "Capacitor"

Thanks in advance.
 
T

Thomas Wieczorek

Hi
/^abc/ =~ "!abc"
returns nil
"You can use an anchor to match the beginning of a string"

Just like the definition says ^ is used to mark the beginning of a string:
/^abc/ =~ "abc!"
will return 0(the start of your pattern)

/abc$/ =~ "abc!"
returns nil
"You can use an anchor to match the end of a string"

$ is used to mark the end of a string:
/abc$/ =~ "!abc"
will return 0 again.

/^I love Ruby$/ will only match the string "I love Ruby" and not "He
said: I love Ruby". /I love Ruby/ or /I love Ruby/$ will match /^I
love Ruby$/
 
C

Corey Jewett

Hi


Just like the definition says ^ is used to mark the beginning of a
string:
/^abc/ =~ "abc!"
will return 0(the start of your pattern)



$ is used to mark the end of a string:
/abc$/ =~ "!abc"
will return 0 again.

/^I love Ruby$/ will only match the string "I love Ruby" and not "He
said: I love Ruby". /I love Ruby/ or /I love Ruby/$ will match /^I
love Ruby$/

^, $, \A, \Z, \B, etc. are called anchors.

Corey
 
B

Bertram Scharpf

Hi,

Am Freitag, 06. Jul 2007, 14:33:12 +0900 schrieb Thomas Wieczorek:
Just like the definition says ^ is used to mark the beginning of a string:
/^abc/ =~ "abc!"

Another time:

^ and $ match the beginning/end of a ___line___
\A and \z match the beginning/end of a ___string___

This makes a difference when you string contains newline
characters.

"abc\nde\nfgh\n".scan /^./ # => ["a", "d", "f"]
"abc\nde\nfgh\n" =~ /.$/ # => 2
$& # => "c"

I would have guessed that /\z/ is faster than /$/ but /$/ is
approximately 10 times faster here. Does anybody know why?

Bertram
 
R

Robert Dober

/^abc/ =~ "abc!"
will return 0(the start of your pattern)



$ is used to mark the end of a string:
/abc$/ =~ "!abc"
will return 0 again.
A *very large* value of 0 that is ;)

Robert
 
J

Jim Clark

Al said:
Thank you very much guys. This topic is much clearer now.
To learn more, a book I highly recommend is "Mastering Regular
Expressions" by Jeffrey Friedl.. Although the examples are written in
Perl (at least the first edition copy that I have), translating any of
these to Ruby should prove to be trivial. The time you spend reading the
book will save you many hours when you need to create, debug and/or
optimize more complex expressions.

Regards,
Jim
 
J

John Joyce

To learn more, a book I highly recommend is "Mastering Regular
Expressions" by Jeffrey Friedl.. Although the examples are written
in Perl (at least the first edition copy that I have), translating
any of these to Ruby should prove to be trivial. The time you spend
reading the book will save you many hours when you need to create,
debug and/or optimize more complex expressions.

Regards,
Jim
I second that book, I read some of it years ago, and it is
fascinating stuff!
Works well as a cookbook for regex's!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top