[wellylug] Regex, replace stuff in command line help...

Grant McLean grant at mclean.net.nz
Thu Jun 29 10:57:19 NZST 2006


On Thu, 2006-06-29 at 10:43 +1200, Jo Booth wrote:
> On 29/06/2006, at 10:27 , David Antliff wrote:
> 
> > perl -ne 'print "$1\n" if /profile\?u\=(.*)/' *

I would tend to write that as:

  perl -nle '/profile\?u=(.*?)/ && print $1' *

Where the -l adds a newline to every print (which as it turns out the OP
didn't want) and saves having to do the "\n" thing.

Using '.*' in a Perl regex is frequently an error.  I added the trailing
'?' which causes it to find the first match rather than the longest.

> perl -ne 'print "$1," if /profile\?u\=([a-zA-Z0-9_\-]+)\"/'  
> match8.html > done.txt

  [a-zA-Z0-9_\-]

Could also be written as:

  [\w-]

Where \w is the 'class' of 'word' characters.  That is characters which
are allowable in Perl identifiers (variable names etc).  This class
contains letters, numbers and the underscore.

You don't have to escape the '-' if it's the last thing in the square
brackets.

Cheers
Grant




More information about the wellylug mailing list