Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

March a regex using split function

by sreek3502 (Novice)
on Sep 13, 2017 at 02:25 UTC ( [id://1199251]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to extract only the FREE HEAP count value which is 1400 from the below line. Here i'm using split function twice to get the value. Can somone help me to get a single regex that would extract the value 1400 using split function here.

my @split_val = split(',',$line); print $split_val[2]; my @split_val1= split(':',$split_val[2]);

input text

2014-04-23 14:66:87,666 INFO - HEAP - [USAGE: 1948.3, FREE: 1400, MAX +: 3597.5]; NON-HEAP - [USAGE: 611.9, FREE: 52.2, MAX: 1776.0]; CLASSE +S - [Loaded: 67007, Unloaded: 3237, Left: 63770]; THREADS - [Count: 3 +42]

Output: 1400

2017-09-19 Athanasius put code tags around the input text

Replies are listed 'Best First'.
Re: March a regex using split function
by AnomalousMonk (Archbishop) on Sep 13, 2017 at 02:34 UTC

    c:\@Work\Perl\monks>perl -wMstrict -le "my $s = '2014-04-23 14:66:87 HEAP - [USAGE: 1948.3, FREE: 1400]; NON- +HEAP - [USAGE: 611.9, FREE: 52.2]'; ;; my ($f) = $s =~ m{ FREE: \s* (\d+) }xms or die 'no FREE'; print qq{'$f'}; " '1400'


    Give a man a fish:  <%-{-{-{-<

      m{ FREE: \s* (\d+) }xms

      Do we really require \s* and could you please tell me why did you gave xms

        You could probably use something like this:
        my ($f) = $s =~ m{FREE: (\d+)} or die 'no FREE';
        But AnomalousMonk's proposal is arguably more readable.

        Also, the \s* makes is possible to cope with possible variations in the number of spaces between FREE: and the value. Perhaps you don't need it if you can be absolutely sure that there will always be one and only one space there, but can you be that sure?

        I've no idea what "our" requirement is. My practice is to deploy defensive whitespace in situations like this.

        Always /xms:

        • /x always use regex whtespace for comments;
        • /m ^ $ always match embedded newlines;
        • /s dot always matches all.
        These regex behavior modifications reduce the "degrees of freedom" of regexes — less to think about.


        Give a man a fish:  <%-{-{-{-<

Re: March a regex using split function
by BillKSmith (Monsignor) on Sep 13, 2017 at 13:29 UTC
    A single example does not give us enough information to parse your input. My questions go far beyond the whitespace issues already discussed. All replies given so far assume that you want the first number after the first 'FREE'. Is that always true? Perhaps you need the first number after the first 'FREE' after the first 'HEAP' which is not preceded by 'NON-'. The number in your example is an integer. Always true? Can there be a newline anywhere in the record? The more that you tell us about your input, the better answer you are likely to receive.
    Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1199251]
Approved by beech
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-19 20:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found