http://www.perlmonks.org?node_id=1116295


in reply to Perl regex match

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...