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

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.
      Cheers,
      Sinan
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:
    s/608,132//;
    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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://17216]
Approved by root
help
Chatterbox?
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
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?