Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Reading Peter Cooper's book; about regular expressions

Reply
Thread Tools

Reading Peter Cooper's book; about regular expressions

 
 
Kaye Ng
Guest
Posts: n/a
 
      08-30-2010
x = <<MY_DELIMITER
We like Ruby
They adore Ruby
Everyone loves Ruby
MY_DELIMITER

#changes the first line only (from 'We'to 'Do you')
puts x.sub(/^../, 'Do you')

puts

#ALSO changes the first line only, but anchors at the end of the line
(from 'Ruby' to 'Rubic')
puts x.sub(/.$/, 'ic')

--------------------------------------------------------------------------------
So what does the author mean by "beginning of any lines" in page 45,
third paragraph of his book?

"The ^ is an anchor, meaning the regular expression
will match from the beginning of any lines within the string."

I'm also having a bit of trouble using the syntax \A and \Z. Can
anybody show how it's done please?

Thank you very much.
Peter Cooper Rocks!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Bob Proulx
Guest
Posts: n/a
 
      08-30-2010
Kaye Ng wrote:
> x = <<MY_DELIMITER
> We like Ruby
> They adore Ruby
> Everyone loves Ruby
> MY_DELIMITER
> ...
> So what does the author mean by "beginning of any lines" in page 45,
> third paragraph of his book?
>
> "The ^ is an anchor, meaning the regular expression
> will match from the beginning of any lines within the string."


The ^ anchors the regular expression to the beginning of the line. In
the above example there are three lines. You can see where they are
by this example which replaces the "nothing" at the beginning of every
line with an 'X' character.

puts x.gsub(/^/, 'X')
XWe like Ruby
XThey adore Ruby
XEveryone loves Ruby

The location of the 'X's mark the beginning of the lines.

More rigoriously a line begins at the beginning or after any newline
character.

> I'm also having a bit of trouble using the syntax \A and \Z. Can
> anybody show how it's done please?


The \A matches only at the beginning of the string. This ignores
embedded newlines.

puts x.gsub(/\A/, 'X')
XWe like Ruby
They adore Ruby
Everyone loves Ruby

The \Z matches only at the end of the string, or before a newline at
the end. This is unfortunately inconsistent because it will match
twice at the end if there is a newline at the end.

puts x.gsub(/\Z/, 'X')
We like Ruby
They adore Ruby
Everyone loves RubyX
X

This is unfortunate but compatible with PCRE (perl compatible regular
expression). If the ending newline is stripped off then this matches
only at the end.

puts x.strip.gsub(/\Z/, 'X')
We like Ruby
They adore Ruby
Everyone loves RubyX

Bob

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom Regular Expressions in ASP.net Jay Douglas ASP .Net 3 11-03-2003 08:09 PM
Regular expressions mark Perl 4 10-28-2003 12:37 PM
perl regular expressions return last matched occurence? Dustin D. Perl 1 08-28-2003 01:51 AM
Add custom regular expressions to the validation list of available expressions Jay Douglas ASP .Net 0 08-15-2003 10:19 PM



Advertisments