开发者

Regular expression to match last item without delimiter

开发者 https://www.devze.com 2022-12-22 20:37 出处:网络
I have a string of names like this \"J. Smith; B. Jones; O. Henry\" I can match all but the last name with

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")
0

精彩评论

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