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

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.

···

--
Posted via http://www.ruby-forum.com/.

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$/

Hello,

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

Can someone please clarify?

In addition to the answers already given, maybe you should check out this
page:

I've found the parent site is a pretty decent reference for regular
expressions in general.

Shajith

PS: Also note that the '^' character has a different meaning when used in a
character class.
Read about them here:

···

On 7/6/07, Al Cholic <desertfox@hot.ee> wrote:

Also, if the thread's title has meaning, a "^" is called a caret.

···

--
Posted via http://www.ruby-forum.com/.

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

Corey

···

On Jul 5, 2007, at 10:33 PM, Thomas Wieczorek wrote:

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$/

Thank you very much guys. This topic is much clearer now.

···

--
Posted via http://www.ruby-forum.com/.

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!"

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

···

Am Freitag, 06. Jul 2007, 14:33:12 +0900 schrieb Thomas Wieczorek:

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

A *very large* value of 0 that is :wink:

Robert

···

On 7/6/07, Thomas Wieczorek <wieczo.yo@googlemail.com> wrote:

/^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 always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

Ooops, should be 1. Sorry

···

2007/7/6, Robert Dober <robert.dober@gmail.com>:

On 7/6/07, Thomas Wieczorek <wieczo.yo@googlemail.com> wrote:>

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

Souleymane Tounkara wrote:

Dear Sir,

<<stuff here>>

THANKS FOR YOUR CO- OPERATIONS.
BEST REGARDS
SOULEYMANE TOUKARA
reply via this email: slt4u2007@yahoo.com

It seems that we were spammed with a classic internet con. How do we
get this junk removed?

···

--
Posted via http://www.ruby-forum.com/\.

Al Cholic wrote:

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

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

···

On Jul 6, 2007, at 1:03 PM, Jim Clark wrote:

Al Cholic wrote:

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