Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: how to split the string into two or three numbers based on certain value.

by moritz (Cardinal)
on Feb 11, 2013 at 20:51 UTC ( #1018239=note: print w/ replies, xml ) Need Help??


in reply to how to split the string into two or three numbers based on certain value.

If you really want a regex-solution (which isn't necessarily the best idea), here's one from the bottom of the poison cabinet:

use strict; use warnings; use 5.010; $_ = '79899920179'; my $pass = qr/(?=)/; my $fail = qr/(*FAIL)/; while (/\G(\d{2,}?)(??{ $1 >= 32 ? $pass : $fail })/g) { say $1; }

I'm sure that can be simplified somehow, but right now I don't know how.

Be sure to read the warning about (??{...}) in perlre.

And here's a nicer Perl 6 solution:

$ perl6 -e 'say "79899920179".comb(/ (\d **? 2..*) <?{ $0 >= 32 }>/)' 79 89 99 201 79


Comment on Re: how to split the string into two or three numbers based on certain value.
Select or Download Code
Re^2: how to split the string into two or three numbers based on certain value.
by AnomalousMonk (Abbot) on Feb 11, 2013 at 23:47 UTC

    A slight simplification. I'm not sure the  \G is necessary: the given test string produces the same groups with/without it. (Update: And the same caveat about a regex solution possibly not being optimum.) See Extended Patterns in perlre.

    >perl -wMstrict -le "my $s = '79899920179'; ;; my @n = $s =~ m{ \G (\d{2,3}?) (?(?{ $^N < 32 }) (*FAIL)) }xmsg; printf qq{'$_' } for @n; " '79' '89' '99' '201' '79'
      Hi this works perfectly fine. Can you explain me the regulsr expression as i am nee to perl programming
        Can you explain me the regulsr expression ...

        I can begin to explain it and then pass you off to some explanations in the docs that do a better job than I could.

        •  \G   Begin a 'global' search (i.e., a search controlled by the  /g regex modifier) at the exact position where the previous search left off, or at the start of the string (same as \A) if there was no previous search.
        •  (\d{2,3}?)   Capture two or three decimal digits. Because the  ? 'lazy' match modifier controls matching, try first to capture two digits (normal matching would be 'greedy' and try to grab three).
        •  (?(?{ $^N < 32 }) (*FAIL))   If the two digits captured by the immediately previous capture group (see  $^N   in perlvar) are numerically less than 32, fail that match and go back and try for three digits. Here's where I bail out. See Extended Patterns in perlre and search for the "(?(condition)yes-pattern)" discussion; also check the Conditional expressions section in perlretut. Check Special Backtracking Control Verbs in perlre for info on (*FAIL); see also the Backtracking control verbs discussion in perlretut. (Note that  (*FAIL) can be replaced by  (?!) if you're scared by the "WARNING: These patterns are experimental and subject to change..." language prefacing documentation of these verbs.)
        HTH.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-09-21 13:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (169 votes), past polls