in reply to [untitled node, ID 192753]

If you're going to do this using a regex you're going to need to use m's parentheses matching capabilities.

For the example text you posted, this does work.

#!/usr/bin/perl -w use strict; $line = '<!-- USER 20 - donkey_pusher_6 -->'; print $1 if $line =~ m/\s-{1}\s(\w.+)\s/i;

What it is saying is, look for a single dash surronded by spaces. Then the next alphanumeric characters up until the next space are stored in $1. That's where the parenthesis come in with your match. If you have more than one set of parens, then your matches are stored in $2, $3, etc...

I am going on the assumption here that all your data is in that format. If not, then hopefully that will give you a start in the right direction.

Good luck!

Some people fall from grace. I prefer a running start...

Replies are listed 'Best First'.
Re: Re: Regex simplification
by Cody Pendant (Prior) on Aug 26, 2002 at 03:22 UTC
    I'd do it like this:
    $line = '<!-- USER 20 - donkey_pusher_6 -->'; print $1 if $line =~ m/<!-- USER \d+ - ([^\s]+)/i;
    because you might get false-positive matches the other way.
    --
    ($_='jjjuuusssttt annootthheer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;
      print $1 if $line =~ m/<!-- USER \d+ - ([^\s]+)/i;
      This is good, but it \S is shorter than [^\s], so:
      print $1 if $line =~ m/<!-- USER \d+ - (\S+)/i;
      Although to get a little closer to the original specification, I'd put:
      my $user=undef; for (@site) { if (/<!--.*USER.* (\S+) -->/) { print "joe:\n"; $user = $1; last; } } print "user = $user\n";