Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Can we assume that <nth line> really is just your notation for the beginning of a new line, where lines end with "\n"?

If that's the case:

use strict; # Don't comment it out. open RERUN, "rerunlrg" or die "Couldn't open input file.\n$!"; while ( my $line = <RERUN> ) { print "Matched:\n$1\n\n" if $line =~ m/^(\S+\s+\S+)/; } close RERUN;

Go ahead and give that a try.

One problem your regexp has is that you're not putting a quantifier after your \w metachars. That means that you're trying to match a single word character, followed by any positive amount of whitespace, followed by a single word character, followed by a single whitespace, followed by any amount of anything. You really want to be matching words of arbitrary length, presumably longer than a single character each.

Another problem your script has is that you're not checking for success when you open the file. You should probably be invoking die if there is a failure to open the file. See the example I provided. You can read up on this issue in perlopentut.


Dave


In reply to Re: first two words-pattern matching by davido
in thread first two words-pattern matching by shamala

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-19 20:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found