Reputation: 391
Question: How do I get a regex wildcard to select only between the two nearest elements instead of going on to further similar elements?
I have a VERY long list of names that need to have the last name separated from the rest by a tab character. In order to accomplish this, I've inserted a placeholder character (#) just before the return of each line, and replaced all spaces in the names with a different placeholder character (@) for my regex to reference. These names are all first, middle initial, then last, having a space on each side of the middle initial. So, with my placeholder characters inserted, the list looks like this (short sample):
Edward@C.@Sellner#
James@J.@Megivern#
J.@Philip@Newell#
I need to isolate only the last name between the @ and # placeholders. When I try and do that, my regex is going to the first @ placeholder, instead of stopping at the second @ placeholder, just before the last name. Here's what I'm using in my search:
@([\s\S]*?)#
which I would replace with:
(tab)$1#
after which I would convert all placeholders back to their original state.
What should I be doing differently here?
Thanks.
Upvotes: 1
Views: 51
Reputation: 626709
You may use
@([^@#]*)#
See the regex demo
Details
@
- a @
char([^@#]*)
- Group 1: 0 or more chars other than @
and #
#
- a #
char.See the regex graph:
Upvotes: 1