tags:

views:

65

answers:

3

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?

+5  A: 

This does it here:

\w+.*?(?:;|$)
Ignacio Vazquez-Abrams
I think I understand what ;|$ is for, but I don't get what the ?: is doing. Could you explain?
Steven
The `(?:...)` is a non-matching group. It won't show up via a `\<n>` backreference.
Ignacio Vazquez-Abrams
Ok, thanks. Would `\w+.*?(?:[;\$])` work the same way?
Steven
No. `$` in a range matches a literal `$`, not the end of the line.
Ignacio Vazquez-Abrams
That's a **non-capturing** group, not *non-matching*. http://www.regular-expressions.info/brackets.html
Alan Moore
+3  A: 

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']
ghostdog74
+1 for keeping it simple. I think the OP wanted all three names, so just `mystring.split(';')` would suffice.
Adam Bernier
thks. i misread the requirement
ghostdog74
+2  A: 

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