Thanks Philip for sharing this excellent piece of information. Cheers, Parag
On Tue, Dec 22, 2009 at 8:17 PM, Philip Potter <philip.g.pot...@gmail.com>wrote: > 2009/12/22 Parag Kalra <paragka...@gmail.com>: > > You can try following: > > > > $_ =~ s/^\.(\s)+//g; > > This isn't quite right. > > There are two ways in which you might use this substitution: either $_ > will contain a single line, or it will contain multiple lines. The > single line case might look something like this: > > while (<>) { > s/^\.(\s)+//g; > print; > } > > In this case, however, the /g modifier is redundant and confusing, > since you only want the regex to match once at the start of each line. > Take the /g modifier off. > > The multiple line case might look like this: > > $_ = do {local $/; <>;}; # but better to use File::Slurp > s/^\.(\s)+//g; > print; > > This code is broken. The problem is that ^ matches start-of-string, > not start-of-line. Therefore, even with the /g modifier the regex can > only match at the beginning of the string, and so will only match > once. > > To change ^ to match start-of-line, you need the /m modifier: > > s/^\.(\s)+//gm; > print; > > Perl Best Practices makes the interesting recommendation that all > regexes should use the /m modifier, because having ^$ match line > boundaries seems to be what most programmers expect and seems to be > useful much more often. If you still need to match start and end of > string you can use \A and \z. > > Phil >