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


#1

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.


#2

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


#3

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


#4

Hello,

On 7/6/07, Al Cholic removed_email_address@domain.invalid 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:
http://www.regular-expressions.info/anchors.html

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:
http://www.regular-expressions.info/charclass.html


#5

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


#6

On 7/6/07, Thomas W. removed_email_address@domain.invalid wrote:

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

Robert


#7

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: removed_email_address@domain.invalid


#8

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


#9

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: removed_email_address@domain.invalid


#10

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: removed_email_address@domain.invalid


#11

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: removed_email_address@domain.invalid


#12

2007/7/6, Robert D. removed_email_address@domain.invalid:

On 7/6/07, Thomas W. removed_email_address@domain.invalid 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


#13

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


#14

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


#15

Souleymane Tounkara wrote:

Dear Sir,

<>

THANKS FOR YOUR CO- OPERATIONS.
BEST REGARDS
SOULEYMANE TOUKARA
reply via this email: removed_email_address@domain.invalid

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


#16

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!