Hi --

On Mon, 17 Mar 2008, Xavier Noria wrote:

> On Mar 16, 2008, at 21:23 , Adam Akhtar wrote:
>
>> I haven't seen the /mx term before, is that reponsible for not including
>> the markers themselves?
>
> Those are two regexp modifiers stacked together:
>
> * With /m the dot matches newlines. I couldn't assume the text to extract 
> doesn't contain newlines so I added it just in case.
>
> * With /x literal whitespace in the regexp are ignored. Since the regexp uses 
> so many backslashes that gives some air for readability.

I know I'm in the minority, but I'll just mention that I find most
regexes that make use of /x very hard to read. The reason is that I've
trained my brain how to read a pattern, so if I encounter this:

   / string (.*) another ? string /x

it's a considerable effort to "not see" the spaces. I think it's
better to stick to the basic pattern language, which after all is the
only set of rules that we all learn and all share.

I would recommend saving /x for cases where you want to break a regex
out into multiple lines and include comments:

   re = /
          \(              # opening paren
          \d{3}           # area code
          \)              # closing paren

etc. (Not a great example of an obscure pattern that's made more clear
by /x but you get the idea.)


David

-- 
Upcoming Rails training from David A. Black and Ruby Power and Light:
   ADVANCING WITH RAILS, April 14-17 2008, New York City
   CORE RAILS, June 24-27 2008, London (Skills Matter)
See http://www.rubypal.com for details. Berlin dates coming soon!