Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Print the matched pattern

by kingjamesid (Acolyte)
on Oct 06, 2009 at 16:43 UTC ( [id://799527]=perlquestion: print w/replies, xml ) Need Help??

kingjamesid has asked for the wisdom of the Perl Monks concerning the following question:

Hello. From a.txt, find if a pattern matches, if matches found, copy the MATCHED pattern into time.txt. But how can i do that? PRINT OUT ?? Note: the regex format is correct.
$a = 'a.txt'; open(IN,$a) || die "cannot open $a for reading: $!"; $b = 'time.txt'; open(OUT,">>$b") || die "cannot create $b: $!"; while (<IN>) { # read a line from file $a into $_ #print OUT $_; if (/returns \d{1} \(in \d{1}.\d{6} secs/) { print OUT ?? ; } } close(IN) || die "can't close $a:$!"; close(OUT) || die "can't close $b:$!";

Replies are listed 'Best First'.
Re: Print the matched pattern
by johngg (Canon) on Oct 06, 2009 at 17:42 UTC

    A few points about your code.

    • In your pattern \d{1} is exactly the same as saying \d, IOW a quantifier of one is implicit and needn't be stated.

    • Be aware that a full stop is a regular expression metacharacter matching any character (with caveats, see perlre) so did you mean to match a decimal point in this pattern /returns \d{1} \(in \d{1}.\d{6} secs/? Consider this code

      $ perl -le ' > print > m{\d{1}.\d{6}} > ? qq{$_: Matched} > : qq{$_: No match} > for > q{in 1.234567 secs}, > q{in 12345678 secs}, > q{in 3A987654 secs};' in 1.234567 secs: Matched in 12345678 secs: Matched in 3A987654 secs: Matched $

      You will need to escape the dot if you want a decimal point

      $ perl -le ' > print > m{\d{1}\.\d{6}} > ? qq{$_: Matched} > : qq{$_: No match} > for > q{in 1.234567 secs}, > q{in 12345678 secs}, > q{in 3A987654 secs};' in 1.234567 secs: Matched in 12345678 secs: No match in 3A987654 secs: No match $

      Now only a literal full-stop is matched.

    • Consider using the three-argument form of open with lexical filehandles.

    • Avoid using the variable names $a and $b as they are special and should be reserved for use in sort routines.

    I hope these points are helpful.

    Cheers,

    JohnGG

Re: Print the matched pattern
by moritz (Cardinal) on Oct 06, 2009 at 16:53 UTC
    See $& (and also the caveats described there).
    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Print the matched pattern
by bichonfrise74 (Vicar) on Oct 06, 2009 at 16:55 UTC
    This example might help you.
    #!/usr/bin/perl use strict; my $string = 'to test me'; $string =~ /to\s(\w+)\s/; print $1;
    Please see perlre for more information specifically under capturing buffers.
      Thanks. I didnt know, $1 is carried over. Dumb me though I thought about it. Thanks
Re: Print the matched pattern
by toolic (Bishop) on Oct 06, 2009 at 16:53 UTC
    You need to provide more details on your problem.
    Show a few relevant lines of your input file.
    Show what you expect to be in your output file.
    Show any warning/error messages you may have gotten.
    Do you use strict and warnings?
Re: Print the matched pattern
by BioLion (Curate) on Oct 06, 2009 at 16:54 UTC

    Check perlre's "Capture buffers". Also Use strict warnings and diagnostics or die!

    use strict; use warnings; my $a = 'a.txt'; open(my $in ,'<', $a) || die "cannot open $a for reading: $!"; my $b = 'time.txt'; open (my $out,">>$b") || die "cannot create $b: $!"; while (<$in>) { # read a line from file $a into $_ #print $out $_; if (/(returns \d{1} \(in \d{1}.\d{6} secs)/) { ## use capturing print $out $1."\n" ; } } close($in) || die "can't close $a:$!"; close($out) || die "can't close $b:$!";
    Just a something something...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://799527]
Approved by moritz
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: (2)
As of 2024-04-25 06:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found