Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: extract the value before a particualr string

by BillKSmith (Chaplain)
on Sep 05, 2013 at 11:57 UTC ( #1052551=note: print w/ replies, xml ) Need Help??


in reply to extract the value before a particualr string

The regex in the following script does exactly what you specify. It extracts the 5-digit value before each "\total" in a single line.

use strict; use warnings; my $in_string = "Apples Orances:5433 55552246:777449 Country 457852/total" . " " . "Apples Red:9987 green:777449 Public 74585/total" . "\n" ; my @totals = $in_string =~ m{ (\d{5}) (?= /total ) }gx; print "@totals\n";
Bill


Comment on Re: extract the value before a particualr string
Download Code
Re^2: extract the value before a particualr string
by 2teez (Priest) on Sep 05, 2013 at 12:18 UTC

    Hi,
    with ...(\d{5})... in your regexp, you will only have 57852 instead of 457852 for the first value, since the digits are more that 5.
    Maybe ...(\d{5,})... could help in this case. Or other variation in case the targeted digits are less than 5.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2014-09-22 23:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (208 votes), past polls