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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C5b20b09d5d24bd554f703ff0595696b?d=identicon&s=25 Al Cholic (desertfox)
on 2007-07-06 07:18
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.
88de1beaaacae26b839f2e6aebf857b6?d=identicon&s=25 Thomas Wieczorek (Guest)
on 2007-07-06 07:34
(Received via mailing list)
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$/
05582fbf0c0caecefda1b40ac249b3ab?d=identicon&s=25 Corey Jewett (Guest)
on 2007-07-06 07:54
(Received via mailing list)
On Jul 5, 2007, at 10:33 PM, Thomas Wieczorek 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
F0e2e7181221d362a226abe48d46e0e0?d=identicon&s=25 Shajith C T (Guest)
on 2007-07-06 08:08
(Received via mailing list)
Hello,

On 7/6/07, Al Cholic <desertfox@hot.ee> 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
C5b20b09d5d24bd554f703ff0595696b?d=identicon&s=25 Al Cholic (desertfox)
on 2007-07-06 09:10
Thank you very much guys.  This topic is much clearer now.
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2007-07-06 10:15
(Received via mailing list)
Hi,

Am Freitag, 06. Jul 2007, 14:33:12 +0900 schrieb Thomas Wieczorek:
> >/^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
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-07-06 12:15
(Received via mailing list)
On 7/6/07, Thomas Wieczorek <wieczo.yo@googlemail.com> wrote:

> will return 0 again.
A *very large* value of 0 that is ;)

Robert
26649df554a849693b69dac2e0abd225?d=identicon&s=25 Souleymane Tounkara (tounkara)
on 2007-07-06 13:18
 DearThomas Wieczorek,


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: slt4u2007@yahoo.com
26649df554a849693b69dac2e0abd225?d=identicon&s=25 Souleymane Tounkara (tounkara)
on 2007-07-06 13:21
 Dear Corey Jewett


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: slt4u2007@yahoo.com
26649df554a849693b69dac2e0abd225?d=identicon&s=25 Souleymane Tounkara (tounkara)
on 2007-07-06 13:23
 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: slt4u2007@yahoo.com
26649df554a849693b69dac2e0abd225?d=identicon&s=25 Souleymane Tounkara (tounkara)
on 2007-07-06 13:26
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: slt4u2007@yahoo.com
88de1beaaacae26b839f2e6aebf857b6?d=identicon&s=25 Thomas Wieczorek (Guest)
on 2007-07-06 13:57
(Received via mailing list)
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 ;)
>
Ooops, should be 1. Sorry
84dc575c33a123789521d53cad0f62ae?d=identicon&s=25 Lloyd Linklater (lloyd)
on 2007-07-06 15:16
Also, if the thread's title has meaning, a "^" is called a caret.
84dc575c33a123789521d53cad0f62ae?d=identicon&s=25 Lloyd Linklater (lloyd)
on 2007-07-06 15:18
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?
966eb34bbc82c352d9e2e43ce5e8905d?d=identicon&s=25 Jim Clark (Guest)
on 2007-07-06 20:05
(Received via mailing list)
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
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 John Joyce (Guest)
on 2007-07-06 22:42
(Received via mailing list)
On Jul 6, 2007, at 1:03 PM, Jim Clark 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!
This topic is locked and can not be replied to.