Browse - Programming Tips - How can I extract the first word in a string?

Date: 2017jun15
Language: Perl

Q.  How can I extract the first word in a string?

A.

sub extractFirstWord($) { my($s) = @_; if ($s !~ m/^(\w+)/) { return '' } return $1; }
We use the not-match operator !~ If the regex of \w+ doesn't match we return '' Otherwise the first word is in $1