[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Kristen

7/6/2007 5:18:00 AM

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-....

10 Answers

Thomas Wieczorek

7/6/2007 5:33:00 AM

0

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

Corey Jewett

7/6/2007 5:53:00 AM

0

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

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

Corey


Kristen

7/6/2007 7:11:00 AM

0

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


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

Bertram Scharpf

7/6/2007 8:14:00 AM

0

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


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

Robert Dober

7/6/2007 10:14:00 AM

0

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.
A *very large* value of 0 that is ;)

Robert


--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

Thomas Wieczorek

7/6/2007 11:56:00 AM

0

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

Lloyd Linklater

7/6/2007 1:17:00 PM

0

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

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

Lloyd Linklater

7/6/2007 1:19:00 PM

0

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-....

Jim Clark

7/6/2007 6:04:00 PM

0

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

John Joyce

7/6/2007 8:41:00 PM

0


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
>
I second that book, I read some of it years ago, and it is
fascinating stuff!
Works well as a cookbook for regex's!