LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-22-2011, 08:10 AM   #1
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Rep: Reputation: 36
perl improve regex query


hi,

I want to get rid of the repetitive \s+(\d+) in my pattern.can any one suggest a better way of doing this.Thanks.



Code:
#!/usr/bin/perl
use strict;
use warnings;
my $mem=`cat /proc/meminfo`;
my @arr=$mem=~m/MemTotal:\s+(\d+).*MemFree:\s+(\d+).*SwapTotal:\s+(\d+).*SwapFree:\s+(\d+)/s;
map { print $_."\n" } @arr;
To get all the digits i can do this m/.*:\s+(\d+).*/g

Last edited by kdelover; 02-22-2011 at 08:17 AM.
 
Old 02-22-2011, 10:28 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Code:
my @arr=$mem=~m/(?:MemTotal|MemFree|SwapTotal|SwapFree):\s+(\d+)/gxs;

Cheers,
Tink
 
Old 02-22-2011, 12:17 PM   #3
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Original Poster
Rep: Reputation: 36
Thanks its works! i had to dig a bit to see what exactly you were doing as i am new to perl & regex

i wanted to clear few things,the reason for using ?: is not to save the matched pattern in the memory?So,if you dont use ?: i guess $1 should contain MemTotal,MemFree,SwapTotal,SwapFree. Is that right?

i still find it little tricky how the patterning matching options are xs working,although i understand how g works in the above the example.

Thanks again !!

Last edited by kdelover; 02-22-2011 at 12:20 PM.
 
Old 02-22-2011, 01:46 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by kdelover View Post
Thanks its works! i had to dig a bit to see what exactly you were doing as i am new to perl & regex

i wanted to clear few things,the reason for using ?: is not to save the matched pattern in the memory?So,if you dont use ?: i guess $1 should contain MemTotal,MemFree,SwapTotal,SwapFree. Is that right?
Yup. Correctly assessed.

Quote:
Originally Posted by kdelover View Post
i still find it little tricky how the patterning matching options are xs working,although i understand how g works in the above the example.

Thanks again !!
Heh. I'm sure you'll get the hang of it really soon. For some
in-depth coverage have a look at "Mastering regular expressions" in
your local library (or add it to your personal library) ... awesome
book, w/ a strong focus on RegEx & perl (while it also covers aspects
of RE's in other languages and a variety of regex engines).




Cheers,
Tink
 
Old 02-23-2011, 12:14 AM   #5
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Original Poster
Rep: Reputation: 36
Cool.using only g works too.thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] perl regex query kdelover Programming 13 11-28-2010 08:07 PM
Perl to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 08:08 PM
need help with regex in perl umbrella2 Programming 5 01-15-2009 09:13 AM
Perl regex $ ShaqDiesel Programming 6 08-18-2006 02:40 PM
regex Perl help igotlongestname Programming 2 09-14-2005 07:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration