开发者

unbound variables in perl regexes

开发者 https://www.devze.com 2023-03-06 20:48 出处:网络
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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消