Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

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

You don't mention why split is not an option. I am guessing because you aren't just trying to split a string on some delimiter, you are trying to learn regexes, and this is a problem you feel comfortable with. There is nothing wrong with that, but realize that what pzbagel said was your answer ... your values are stored in the array. What you are trying to do - match some arbitrary numbers of items and populate $1 through $N inside the match operator just doesn't make sense to me. I mean, that's what the g modifier is for ... match all occurances, no matter how many you find.

You hint and Java and Python, but you don't specify what language you are really trying to solve this problem in. If i had to guess, i would say you are using PHP or some Java library that modeled itself against Perl's regexes. Can't help you with the Java stuff, but if it's PHP you are using, then try preg_match_all(). It is like preg_match with Perl's g match modifer, but it's usage is a bit tricky:
<?php $mystr = 'foo,bar,moo,cow'; preg_match_all('/(\w+)\,?/',$mystr,$matches); ?> <ul> <?php foreach ($matches[1] as $match) { ?> <li><?=$match?></li> <?php } ?> </ul>
If you are using Python, then you can use the exact same regex with Python's re.findall():
#!/usr/bin/python from re import findall mystr = 'foo,bar,moo,cow' values = findall('(\w+)\,?',mystr) for val in values: print val
Hope this helps, i feel kinda dirty now ... PHP and Python at a Perl site! ;)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to (jeffa) 3Re: More Variable length regex issues by jeffa
in thread More Variable length regex issues by dextius

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 admiring the Monastery: (3)
As of 2024-04-25 22:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found