Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Question: Here I don't want the $& have whole the value of line. I need $2 only even I omit the extra groups also the $& have value. How can I avoid this?use strict; use warnings; open FH,"data" or die "can't open the file"; while(<FH>) { if(~/^<dsk1/) { my $line=$_; # print $line; $line=~/.*\sid=\"(.*)\"\slo=\"(.*)\"\sto=\"(.*)\"\srb= +.*$/; print "$2\n\n"; print "$&\n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: regular expression.
by biohisham (Priest) on Jul 23, 2010 at 18:49 UTC | |
Be warned that performance is compromised if the $& variable is used any where in the program and that this variable is a read-only variable. Secondly, since you've not provided an example of the lines that you're reading from to perform this match it is a bit unwieldy to try to replicate while you only need $2 then why do you wanna worry about $& in the first place? Does your code take care of when the lines don't match with the above pattern or it generates an uninitialized variable warning when printing $2?
Excellence is an Endeavor of Persistence. A Year-Old Monk :D . | [reply] [d/l] [select] |
|
Re: regular expression.
by JediWizard (Deacon) on Jul 23, 2010 at 18:20 UTC | |
It is only a little more complicated than the previous post implies. The $& varaible will have a value for every regular expression in a program if it is used with any regular expression. The same is also true for $` and $'. They say that time changes things, but you actually have to change them yourself. Andy Warhol | [reply] [d/l] |
|
Re: regular expression.
by ww (Archbishop) on Jul 23, 2010 at 18:42 UTC | |
AnonyMonk Update: and JediWizard have accurate answers If so, note that $& is a special regex variable that -- quoting from Chapter 7 in "Mastering Regular Expressions" (page 299 in my 2nd Ed. paperback) -- "A successful match or substitution sets a variety of global, read-only variables that are always automatically, dynamically scoped. These values never change if a match attempt is unsuccessful, and are alwaysset when a match is successful." (emphasis in the original; but note especially the first clause of the last sentence) Update#2 (See AnomalousMonk's below):
using this, slightly modified code:
Note also that you are using conventional regex notation at line 12 but not in line 9. Update2: Hence, I'm posting code and output with the match syntax (rather than bitwise negation):
</update2a> A few other suggestions:
| [reply] [d/l] [select] |
by AnomalousMonk (Archbishop) on Jul 23, 2010 at 22:34 UTC | |
I'm unclear why line 9 passes a syntax check... Unary ~ is bitwise negation. See Symbolic Unary Operators in perlop. It compiles, but is probably not logically correct. E.g.:
Update: Improved example code. | [reply] [d/l] [select] |
|
Re: regular expression.
by Anonymous Monk on Jul 23, 2010 at 12:51 UTC | |
Don't use $& and it won't have a value, its that simple | [reply] |
|
Re: regular expression.
by Marshall (Canon) on Jul 25, 2010 at 15:33 UTC | |
Stay away from this $& stuff as that will slow all regexes down. And its very seldom needed. If you just need to check if: lo="XYZZY" is on the line, the regex is easier than below. But this a general name="value" solution.
| [reply] [d/l] |