Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Variable matching on a regex

by cdarke (Prior)
on Jun 17, 2010 at 10:55 UTC ( [id://845183]=note: print w/replies, xml ) Need Help??


in reply to Variable matching on a regex

Seems to me you are complicating matters because you consider that spaces follow each field except the last.
So use zero or more spaces instead:
my ($d1, $d2, $d3, $d4, $d5, $d6) = $_ =~ m/(\d+)\s*/g;
Or use word boundaries:
my ($d1, $d2, $d3, $d4, $d5, $d6) = $_ =~ m/\b(\d+)\b/g;

Replies are listed 'Best First'.
Re^2: Variable matching on a regex
by LaintalAy (Sexton) on Jun 17, 2010 at 12:19 UTC

    OK, that works fine, but you're missing my point. That input is just an example, not an actual problem and I agree the regex I'm trying to use is overkilling.

    My question can be summarized on: Is it possible to capture a non fixed number of variables from a "fixed" regex? (without using /g feature). Maybe the answer is just "no", but I wanted to know.

    Cheers,

      You could adapt the code in this node, pushing captures onto an array rather than concatenating them onto a scalar string. It uses regular expression recursion so there are actually two patterns involved rather than one "fixed" regex but the actual match is done just the once without a g flag. Obviously, the global match already shown is a much simpler solution.

      I hope this is of interest.

      Cheers,

      JohnGG

      Why do you want to avoid using /g in the first place?
      How might you possibly define what to capture without specifying all the options or repeating with /g?
      If you provide a pseudocode example, the monks can then come up with the closest real way to do it.

      PS: Whenever you think about declaring $d1, $d2, $d3, what you really want is @d and a more descriptive name.

        Because /g is just the repetition of the regex, and it may not be possible in some circumstances, I think

        It could be perfectly feasible to have a regex like:

        /^\w+\s+(?:(\d+)\s+){3}\w+$/

        So I want a word, 3 group of digits and a word. But right now I don't know how to get the 3 values for the group of numbers. So I usually do something like:

        /^\w+\s+(\d+)\s+(\d+)\s+(\d+)\s+\w+$/

        that doesn't look so good. In extreme cases n can be far bigger than 3 and I should get the whole string of numbers and then split them. Also, if I want instead of just n repetitions to force a threshold ({3,10} for example) then I'm at a loss and I have to implement it in two steps.

        I know that may be even clearer than the regex I'm trying to write, but I'm just curious about it. I'd like my regex to fit as as much as possible the format of my input and get the values straightforwardly. Don't know if it can be done though. That's my question.

        There's no real problem behind, nor real output either. It's just something I've found several times and I've never been happy with the solutions I've implemented.

        Hope it makes more sense now,

        Thanks

      Is it possible to capture a non fixed number of variables from a "fixed" regex? (without using /g feature).

      Sort of:

      @m=(); 'abcdefghijklmnopqrstuvwxyz' =~ m[(?:(?=(..)(?{ push @m, $^N })).)+]; print for @m;; ab bc cd de ef fg gh hi ij jk kl lm mn no op pq qr rs st tu uv vw wx xy yz

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      Maybe the answer is just "no"
      The answer is indeed "no".

Log In?
Username:
Password:

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

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

    No recent polls found