Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Dear Monks, I'm on need of wisdom,

The problem is that I'm having problems trying to use regexes to retrieve matches that are defined as one/none or more. I'm trying with a simple file as example:

1 23 456 789 0123 456 2 24 456 789 0123 456 3 23 456 789 0123 456 4 23 456 789 0123 456 5 23 456 789 0123 456

And I was wondering if it's possible to parse and assign them to variables in just one step. The regex I was trying to use was something like

while (<$fd>) { my $regex = '^(?:(\d+)\s+)+(\d+)$'; (my ($d1, $d2, $d3, $d4, $d5, $d6) = $_) =~ m/$regex/; print "Line $.\n"; print "\t$d1\n"; }

It doesn't work as I'd expect. It matches, but only retrieves the last two elements because instead of getting an array of results for the (?: )+ regex part it stores only the last one.

I know a split would work without that much of a hassle but.. shouldn't be possible to do that just with a regex? I've tried different things without success and I haven't found any relevant example of this.

Thanks,


In reply to Variable matching on a regex by LaintalAy

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 learning in the Monastery: (3)
As of 2024-04-19 01:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found