Reputation: 14671
I want to extract text before first comma (first and last name) from strings like:
John Smith, RN, BSN, MS
Thom Nev, MD
Foo Bar, MD,RN
I tried with regex:
(.*)\s(.*),\s
but this doesn't work for all situations. How to achieve this?
Upvotes: 57
Views: 184563
Reputation: 1127
^([^,])+
This regexp worked for me in Sublime. It selected everything just before the first comma.
Upvotes: 28
Reputation: 1
^[a-zA-Z0-9]+
..use this ... this is the correct answer to get first comma separated value
Upvotes: -5
Reputation: 10926
You have to use non greedy regex operators, try: (.+?),
instead of: (.+),
. Note that when using greedy operators, you'll match everything before the last comma instead of the first.
Upvotes: 6
Reputation: 63540
Match everything from the beginning of the string until the first comma:
^(.+?),
Upvotes: 131
Reputation: 208565
How about the following:
[^,]*
[^,]*
means "match any number of characters that are not commas", which I think it exactly what you are trying to do here. You may want to anchor it to the beginning of the string iwth ^
, like this:
^[^,]*
Upvotes: 13