Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Perl regex match

by manoj_speed (Prior)
on Feb 11, 2015 at 07:14 UTC ( [id://1116295]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1116295]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-19 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found