Steven
Steven

Reputation: 2558

Regular expression to match last item without delimiter

I have a string of names like this "J. Smith; B. Jones; O. Henry" I can match all but the last name with

\w+.*?;

Is there a regular expression that will match all the names, including the last one?

Upvotes: 1

Views: 2186

Answers (3)

Qtax
Qtax

Reputation: 33908

This is simple enough if you want a regex:

\w[^;]+

Perl example:

@names = "J. Smith; B. Jones; O. Henry" =~ /\w[^;]+/g;
# ("J. Smith","B. Jones","O. Henry")

Or if you want a split I'd use \s*;\s* (\s* to remove the spaces):

@names = split /\s*;\s*/, "J. Smith; B. Jones; O. Henry";
# ("J. Smith","B. Jones","O. Henry")

Upvotes: 2

ghostdog74
ghostdog74

Reputation: 342373

don't have to use regex. it seems like between your names ,you have ";" as delimiter, so use that to split on the string. eg Python

>>> mystring = "J. Smith; B. Jones; O. Henry"
>>> mystring.split(";")
['J. Smith', ' B. Jones', ' O. Henry']

Upvotes: 4

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798676

This does it here:

\w+.*?(?:;|$)

Upvotes: 5

Related Questions