LinuxQuestions.org
Help answer threads with 0 replies.
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 06-02-2012, 10:29 AM   #1
sysfce2
Member
 
Registered: Jul 2009
Distribution: Slackware
Posts: 101

Rep: Reputation: 40
Improving perl regex pattern.


Yesterday I stumbled across a way to simplify a common pattern I use:
Code:
if (!defined $variable) {
	$variable='value';
}
can be replaced with:
Code:
$variable//='value';

I was thinking there has to be a way to improve another typical pattern I use:
Code:
if ($value =~ m/^LEVEL [1-5]$/) {
	$value =~ s/^LEVEL ([1-5])$/$1/;
}
I'm thinking that it's kind of wasteful to run basically the same regex twice - any improvement suggestions?
 
Old 06-02-2012, 12:00 PM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,656

Rep: Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851
you may use grouping, something like this:
Code:
if ( $value ~ m/^LEVEL ([1-5])$/ ) {
    $value = $1;
}
(your first example will not simplify the running code, you have only just a bit shorter line)

Last edited by pan64; 06-02-2012 at 12:01 PM.
 
1 members found this post helpful.
Old 06-02-2012, 12:39 PM   #3
sysfce2
Member
 
Registered: Jul 2009
Distribution: Slackware
Posts: 101

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by pan64 View Post
you may use grouping, something like this:
Code:
if ( $value ~ m/^LEVEL ([1-5])$/ ) {
    $value = $1;
}
Thank you - I didn't realize that the $x variables were available after the pattern match: this opens up quite a few possibilities!
One question though, what is the difference between the ~ and =~ operators?

Quote:
Originally Posted by pan64 View Post
(your first example will not simplify the running code, you have only just a bit shorter line)
Yeah, I kind of figured that: but it sure saves on typing.
Same sort of idea for the regexes: while it's nice to have better runtime efficiency, the bigger concern is if I modify one of the regexes I had to make sure and modify the corresponding one too.
 
Old 06-02-2012, 01:06 PM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,656

Rep: Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851
see Binding Operator here: http://perldoc.perl.org/perlop.html
the ~ is my mistake, you need to enter =~. Unary "~" performs bitwise negation, i.e., 1's complement.
 
Old 06-02-2012, 02:14 PM   #5
sysfce2
Member
 
Registered: Jul 2009
Distribution: Slackware
Posts: 101

Original Poster
Rep: Reputation: 40
OK, I also read up on regexes - it all clarifies perl a little more. I've certainly got a long way to go yet though!

OK, I'll go with:
Code:
if ( $value =~ /^LEVEL ([1-5])$/ ) {
    $value = $1;
}
It looks like I can drop the leading m on the regex if I'm using the default / for the regex too.

Thanks again for your help.
 
Old 06-05-2012, 06:44 PM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 200Reputation: 200Reputation: 200
Code:
$value =~ s/^LEVEL ([1-5])/\1/;
($value) = $value =~ /^LEVEL ([1-5])/;
If it fails:
1. will leave $value unaltered
2. will leave it undef

Last edited by bigearsbilly; 06-05-2012 at 06:50 PM.
 
Old 06-09-2012, 07:38 PM   #7
sysfce2
Member
 
Registered: Jul 2009
Distribution: Slackware
Posts: 101

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by bigearsbilly View Post
Code:
$value =~ s/^LEVEL ([1-5])/\1/;
($value) = $value =~ /^LEVEL ([1-5])/;
If it fails:
1. will leave $value unaltered
2. will leave it undef
I'm still trying to make sense of this: can you explain why this works?
 
Old 06-10-2012, 01:24 PM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,656

Rep: Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851
1. if matched $value will be equal to the replaced string otherwise do nothing.
2. match evaluated, result will be equal to value, when no match found the result is undef.








__________________________________
Happy with solution ... mark as SOLVED
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
 
  


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
how to use regex pattern matching to get data from file? ranjit Programming 4 10-17-2011 02:09 PM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
Help with a specific regex pattern in Perl mrwall-e Programming 13 02-28-2011 10:26 AM
Bash - add Pattern in Front of Regex danifunny Programming 5 11-04-2010 10:46 AM
Perl to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 08:08 PM

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

All times are GMT -5. The time now is 04:49 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration