Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: More Variable length regex issues

by pzbagel (Chaplain)
on Jun 08, 2003 at 23:55 UTC ( [id://264187]=note: print w/replies, xml ) Need Help??


in reply to More Variable length regex issues

Count the number of capturing parenthese in your regex's. There's one, hence only $1 gets set. Remember /g simply performs the same regex over and over on the string.

Now a question to you, why are you looking for $2-$4 being set when you should have all the values in the @values array? Why not just reference them there?

HTH

Replies are listed 'Best First'.
Re: Re: More Variable length regex issues
by dextius (Monk) on Jun 09, 2003 at 00:01 UTC
    Two reasons..

    1. I don't fully understand the match operator (even after reading Mastering Regex v2 twice). I'm trying to learn more about how those variables are populated in Perl, compared to Java, or Python...

    2. I'm not exactly using Perl right now. But since I couldn't solve this problem using a Perl regex, it seemed like a decent enough question..

      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)
      

        Sheesh, -1 reputation abounds on this one..

        Ok, I'm parsing USMTF, it has repeatable fields all through it. I can use split, but I have non-repeatable fields that require more delicate processing..

        Doing a split will ignore much of the value of parsing using a regex. The problem is, the target text I am matching against is not fixed length, not even fixed patterns. The last few elements can be repeated infinitely.. I guess I'll go back to mastering regular expressions v2 and see if I missed something..

        Thank you for your time..

        ps. I am forced to using Cold Fusion MX, which is powered by Jrun, which uses the oro module. I prefer Perl to solving problems, but I have to take care of this first..

Re: Re: More Variable length regex issues
by pzbagel (Chaplain) on Jun 09, 2003 at 00:40 UTC

    Well, if you KNOW there will always be 4 fields, then hardcode that in your regex:

    m/^([^,]*),([^,]*),([^,]*),([^,]*)$/;

    A warning: that regex wouldn't work right if there was comma embedded in quotes in one of the fields. I'd recommend using the Text::CSV_XS module, but as you state that you aren't actually using Perl...

    Later

      Yeah, the whole "variable length" part of the string is the problem. Thanks for your help..
Re: Re: More Variable length regex issues
by dextius (Monk) on Jun 09, 2003 at 00:03 UTC
    I guess I'm just looking for something equivalent to "what was matched", as a structure. But not having to catch the other side of the regex as it matches...

Log In?
Username:
Password:

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

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

    No recent polls found