Come for the quick hacks, stay for the epiphanies. | |
PerlMonks |
Re: Perl regex matchby manoj_speed (Prior) |
on Feb 11, 2015 at 07:14 UTC ( [id://1116295]=note: print w/replies, xml ) | Need Help?? |
As LanX mentioned, you can use \w+ to match any word character before 'unit' keyword. Just check the code below: Code # Consider I parsed a file and stored content into below array. my @arr=('line 1 abcunit', 'line 2hedhdunit', 'line 3 ewedfunit', 'line 4 eedunit', 'line 5 abdunited'); for my $str(@arr) { print "Unit : $& \n" if ($str =~ m/\w+unit\b/); } # \w+ -> To match any word character before pattern 'unit' # \b -> to match the exact word ends with 'unit'. Output: Unit : abcunit Unit : 2hedhdunit Unit : ewedfunit Unit : eedunit
--
The wisest mind has something yet to learn...
In Section
Seekers of Perl Wisdom
|
|