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 wi开发者_开发百科ll match all the names, including the last one?
This does it here:
\w+.*?(?:;|$)
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']
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")
精彩评论