[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Omaha.pm] regex questions



>> $adj =~ s/^ $//;

On Apr 17, 2014, at 3:57 PM, Mario Steele <mario@ruby-im.net> wrote:
> That is correct.  It is looking for any lines that have a single space, then an End of Line in it, and replace them with a empty string.
> 
> So, if you have a String that looks like this:
> ["This is a Test.\n",
> " \n",
> "This is a second test.\n"]
> 
> And you run that regex, it'll basically end up like this:
> ["This is a Test.\n",
> "",
> "This is a second test."]

I think your example would only apply if he's looping each line of the input and running the regex against each line. 

If you want to process multiple lines with a single regex call, you'll want to use the /m and /s modifiers:

perldoc perlre

       m   Treat string as multiple lines.  That is, change "^" and "$" from
           matching the start or end of line only at the left and right ends
           of the string to matching them anywhere within the string.

       s   Treat string as single line.  That is, change "." to match any
           character whatsoever, even a newline, which normally it would not
           match.

           Used together, as "/ms", they let the "." match any character
           whatsoever, while still allowing "^" and "$" to match,
           respectively, just after and just before newlines within the
           string.

HTH,

j