Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: March a regex using split function

by AnomalousMonk (Archbishop)
on Sep 13, 2017 at 02:34 UTC ( [id://1199252]=note: print w/replies, xml ) Need Help??


in reply to March a regex using split function

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:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: March a regex using split function
by sreek3502 (Novice) on Sep 13, 2017 at 04:52 UTC

    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?

        You missed a great opportunity to make a Larry Wall style in code pun here...

        or die 'if not FREE'; #William Wallace agrees! :-)

        Just another Perl hooker - My clients appreciate that I keep my code clean but my comments dirty.

      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:  <%-{-{-{-<

        ppixregexplain.pl
        'mods.x' => '(?x) disregarding whitespace and comments', 'mods.-x' => '(?-x) matching whitespace and # normally',

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-04-23 12:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found