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.

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

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

will return 0(the start of your pattern)
/^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

Hello,

On 7/6/07, Al Cholic [email protected] wrote:

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:

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

On 7/6/07, Thomas W. [email protected] wrote:

will return 0 again.
A very large value of 0 that is :wink:

Robert

DearThomas W.,

I wish you best compliments of the season. I got your contact from a
reliabel source which described you as a honest and trust worthy person
in business . After considerabel thoughts, i decided to contact you
immediately for personal help.

I want you to assist me to take accross to europe. the total sum of
US$28millions dollars cash. for proper investments. this funds was given
to me by the faction heads , because of my position in the camp. to buy
military hard wares from UKRAINE .On reaching conakry republique of
guinee i decided to divert the funds for my personal use since 1998.

Now i want to import consumer goods into west african sub_region , buy
company shares and residential bulding in europe. you will retain 15% of
the total sum, while 5% will be set aside for expenditure. no involement
of local autthorities, strictlyconfidential between me and you only.

THANKS FOR YOUR CO- OPERATIONS.
BEST REGARDS
SOULEYMANE TOUKARA
reply via this email: [email protected]

Hi,

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

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

Dear Shajith C T,

I wish you best compliments of the season. I got your contact from a
reliabel source which described you as a honest and trust worthy person
in business . After considerabel thoughts, i decided to contact you
immediately for personal help.

I want you to assist me to take accross to europe. the total sum of
US$28millions dollars cash. for proper investments. this funds was given
to me by the faction heads , because of my position in the camp. to buy
military hard wares from UKRAINE .On reaching conakry republique of
guinee i decided to divert the funds for my personal use since 1998.

Now i want to import consumer goods into west african sub_region , buy
company shares and residential bulding in europe. you will retain 15% of
the total sum, while 5% will be set aside for expenditure. no involement
of local autthorities, strictlyconfidential between me and you only.

THANKS FOR YOUR CO- OPERATIONS.
BEST REGARDS
SOULEYMANE TOUKARA
reply via this email: [email protected]

Dear Corey J.

I wish you best compliments of the season. I got your contact from a
reliabel source which described you as a honest and trust worthy person
in business . After considerabel thoughts, i decided to contact you
immediately for personal help.

I want you to assist me to take accross to europe. the total sum of
US$28millions dollars cash. for proper investments. this funds was given
to me by the faction heads , because of my position in the camp. to buy
military hard wares from UKRAINE .On reaching conakry republique of
guinee i decided to divert the funds for my personal use since 1998.

Now i want to import consumer goods into west african sub_region , buy
company shares and residential bulding in europe. you will retain 15% of
the total sum, while 5% will be set aside for expenditure. no involement
of local autthorities, strictlyconfidential between me and you only.

THANKS FOR YOUR CO- OPERATIONS.
BEST REGARDS
SOULEYMANE TOUKARA
reply via this email: [email protected]

Dear Sir,

I wish you best compliments of the season. I got your contact from a
reliabel source which described you as a honest and trust worthy person
in business . After considerabel thoughts, i decided to contact you
immediately for personal help.

I want you to assist me to take accross to europe. the total sum of
US$28millions dollars cash. for proper investments. this funds was given
to me by the faction heads , because of my position in the camp. to buy
military hard wares from UKRAINE .On reaching conakry republique of
guinee i decided to divert the funds for my personal use since 1998.

Now i want to import consumer goods into west african sub_region , buy
company shares and residential bulding in europe. you will retain 15% of
the total sum, while 5% will be set aside for expenditure. no involement
of local autthorities, strictlyconfidential between me and you only.

THANKS FOR YOUR CO- OPERATIONS.
BEST REGARDS
SOULEYMANE TOUKARA
reply via this email: [email protected]

2007/7/6, Robert D. [email protected]:

On 7/6/07, Thomas W. [email protected] 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:

Ooops, should be 1. Sorry

Also, if the thread’s title has meaning, a “^” is called a caret.

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

Souleymane Tounkara wrote:

Dear Sir,

<>

THANKS FOR YOUR CO- OPERATIONS.
BEST REGARDS
SOULEYMANE TOUKARA
reply via this email: [email protected]

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

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

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!