Need help separating this string of names (first and last names, separated by commas and "and")

I use perl and you need to separate comma-separated lines of author names, as well as the last "and". Names are formed as a first and last name, which are as follows:

$string1 = "Joe Smith, Jason Jones, Jane Doe and Jack Jones";
$string2 = "Joe Smith, Jason Jones, Jane Doe, and Jack Jones";
$string3 = "Jane Doe and Joe Smith";
# Next line doesn't work because there is no comma between last two names
@data = split(/,/, $string1);

I would just like to break the full names into array elements, for example, to split () so that the @data array contains, for example:

@data[0]: "Joe Smith"
@data[1]: "Jason Jones"
@data[2]: "Jane Doe"
@data[3]: "Jack Jones"

However, the problem is that there is no comma between the last two names in the lists. Any help would be appreciated.

+5
source share
2 answers

You can use simple alternation in your regex for split:

my @parts = split(/\s*,\s*|\s+and\s+/, $string1);

For instance:

$ perl -we 'my $string1 = "Joe Smith, Jason Jones, Jane Doe and Jack Jones";print join("\n",split(/\s*,\s*|\s+and\s+/, $string1)),"\n"'
Joe Smith
Jason Jones
Jane Doe
Jack Jones

$ perl -we 'my $string2 = "Jane Doe and Joe Smith";print join("\n",split(/\s*,\s*|\s+and\s+/, $string2)),"\n"'
Jane Doe
Joe Smith

(.. ", " ),

my @parts = split(/\s*,\s*and\s+|\s*,\s*|\s+and\s+/, $string1);

:

$ perl -we 'my $s = "Joe Smith, Jason Jones, Jane Doe, and Jack Jones";print join("\n",split(/\s*,\s*and\s+|\s*,\s*|\s+and\s+/, $s)),"\n"'
Joe Smith
Jason Jones
Jane Doe
Jack Jones

$ perl -we 'my $s = "Joe Smith, Jason Jones, Jane Doe and Jack Jones";print join("\n",split(/\s*,\s*and\s+|\s*,\s*|\s+and\s+/, $s)),"\n"'
Joe Smith
Jason Jones
Jane Doe
Jack Jones

$ perl -we 'my $s = "Joe Smith and Jack Jones";print join("\n",split(/\s*,\s*and\s+|\s*,\s*|\s+and\s+/, $s)),"\n"'
Joe Smith
Jack Jones

stackoverflowuser2010 .

, \s*,\s*and\s+ "" , this :

, , , - , .

+10

split and ,:

$string1 =~ s{\s+and\s+}{,}g;
+4

All Articles