Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Need a regex

by sinan (Sexton)
on Jun 09, 2000 at 01:04 UTC ( #17216=perlquestion: print w/replies, xml ) Need Help??

sinan has asked for the wisdom of the Perl Monks concerning the following question:

I need to filter out the 608,132 in the following line:

USD_ABD DOLARI                608,132   611,064   607,706   611,981   1.0      1.0

Can anybody tell me how I can do it? (Of course, I can always take it out as a substring, but that would not be very reliable, in case the position of the number shifts by one or two.) Thanks in advance, Sinan

Replies are listed 'Best First'.
Re: Need a regex
by plaid (Chaplain) on Jun 09, 2000 at 01:22 UTC
    If this is the general format of the lines you need to extract data from, it appears what you're looking for is just the third field delimited by whitespace. This should work:
    my $string = "USD_ABD DOLARI 608,132 611,064 607,70 +6 611,981 1.0 1.0"; my $value = (split /\s+/, $string)[2];
    Which leaves $value with 608,132.
      Thanks a lot, friar plaid!!!

      This really saved me a lot of trouble.
RE: Need a regex
by merlyn (Sage) on Jun 09, 2000 at 02:44 UTC
    You need to be clear in your question. I can't tell for example that this won't do it:
    Most of the trouble in programming isn't the answers -- it's phrasing the questions properly. Once the question is phrased properly, the answer usually pops out.

    Like, did you mean "I need to remove the third whitespace-delimited word from the string"? Or "I need to remove any word that begins with 608"? Or what?

    -- Randal L. Schwartz, Perl hacker

Re: Need a regex
by chromatic (Archbishop) on Jun 09, 2000 at 01:49 UTC
    I'd definitely use split in this case, but a possible regex would be:
    if ($line =~ /\s+([0-9,]+)\s/) { $number = $1; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://17216]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2020-07-06 06:45 GMT
Find Nodes?
    Voting Booth?

    No recent polls found