Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Perl regex match

by manoj_speed (Prior)
on Feb 11, 2015 at 07:14 UTC ( #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
Node Status?
node history
Node Type: note [id://1116295]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2020-06-01 16:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (5 votes). Check out past polls.

    Notices?