Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: pattern matching

by Corion (Patriarch)
on May 04, 2004 at 10:59 UTC ( [id://350287]=note: print w/replies, xml ) Need Help??


in reply to pattern matching

A nifty trick when your one-liners get out of hand is to use B::Deparse on it:

perl -MO=Deparse -lne 'print if s/0010 0010.*\/\/.*\/\///'

which gives you an approximation of what Perl runs:

LINE: while (defined($_ = <ARGV>)) { chomp $_; print $_ if s[0010 0010.*//.*//][]; }

This is some ugly code, so I'd rewrite that to be more sightly:

#!/usr/perl -w use strict; while (defined(my $line = <ARGV>)) { chomp $line; print $line if s[0010 0010.*//.*//][]; }
or, if you're interested in post-processing the lines in your script:
#!/usr/perl -w use strict; my @interesting_lines = map { s[0010 0010.*//.*//][] ? $_ : ()} (<ARGV>); print "Name: $_" for @interesting_lines;

Update: Corrected last snippet to work like the previous snippets

Replies are listed 'Best First'.
Re: Re: pattern matching
by perlcgi (Hermit) on May 04, 2004 at 13:59 UTC
    Be careful using .* as it is one greedy mother. The first .* will swallow everything up to the second last // in your regex.
    So if your source text ever happens to have three or more sets of // on a line you regex will fail. Better to use nongreedy, minimal matching using .*?
    This is probably so obvious to an expert like Corion, he didn't bother mentioning it. I mention it 'cos the OP states that the regex seems to work.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://350287]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-19 13:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found