Posts Tagged ‘if statement’

Searching within String in Perl

November 10th, 2008

was working on improving a current script in perl and was trying to search through a particular string to get some variables.

For example, an instance in the loop, a string has already been set as $_ = <hkugmdi name=”myname” folder=”myfolder” title=”mytitle> and i want to obtain the name “myname” in every instance of the loop. the below is an if statement which can get the desired input.

if (( $_ =~ m/name\=\”(\w+)/) || ( $_ =~ m/name\=(\w+)/) )

$_ => current string

m => match

name\=\” || name= => match name=” or name= in the string

(\w+) => first word

some other useful syntax

$& => current value

$` => before the string is matched

$’ => after the string is matched

$1 => first grouping

so for the above example,

when i do a print “before value $`, current value $& and after value $’ to get the string desired $1″

i get

before value <hkugmd, current value name=” and after value ” folder=”myfolder” title=”mytitle> to get the string desired myname

VN:F [1.9.6_1107]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.6_1107]
Rating: 0 (from 0 votes)
DiggTwitterTechnorati FavoritesRedditNewsVineFacebookShare
Get Adobe Flash playerPlugin by wpburn.com wordpress themes