Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by AnomalousMonk (Monsignor)
on Feb 11, 2013 at 23:47 UTC ( #1018262=note: print w/ replies, xml ) Need Help??


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

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'


Comment on Re^2: how to split the string into two or three numbers based on certain value.
Select or Download Code
Re^3: how to split the string into two or three numbers based on certain value.
by venky4289 (Novice) on Feb 12, 2013 at 08:19 UTC
    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://1018262]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (21)
As of 2014-07-29 15:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (219 votes), past polls