unbound variables in perl regexes
I have a bunch of lines like these:
John Smith
Jane Doe
Dr. Bruce Wayne
and I would like to put the names into a csv file with two columns: title and full name.
I'm using the regex for this: /(\w*\. )?(.*)/
, then I print开发者_运维问答 "$1;$2"
. The problem is that in names without a title, perl complains about an uninitialized value $1. How do I make it just use an empty string?
Just change your regex to :
my $re = qr/(\w*\. |)(.*)/;
add alternation --^
In general to make part of a match optional you use ?, on a (?: )
group if necessary. Just using ? after a capturing group will leave that capture variable undef if omitted, but you can use a non-capturing group inside the capturing group:
/((?:\w*\. )?)(.*)/;
To address the problem in another way, Lingua-EN-NameParse might help.
精彩评论