Copy link to clipboard
Copied
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 replace the @ placeholder just before the last name with a tab. When I try and do that, my regex is selecting everything between the # placeholder and the first @ placeholder, instead of stopping at the seconds @ 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.
The problem is that this regex is going to the first @ placeholder instead of stopping at the one just before the last name.
What should I be doing differently here?
Thanks.
Copy link to clipboard
Copied
Honestly, this should not be working at all due to \s\S (anything that is a whitespace or anything that isn't a whitespace.)
Let me play around with this a bit. I _love_ RegEx challenges.. usually.
V/r,
^ _ ^
Copy link to clipboard
Copied
BTW.. you don't mention if this is a single string, or an array, or what. What is the context for this RegEx need?
V/r,
^ _ ^
Copy link to clipboard
Copied
Okay.. I was trying to use lookbehinds, but couldn't get it to work.
If you are trying to do this in Dw, use the following:
(@)[^@]*#[\r\n]
Looking for an @ followed by one or more of anything that isn't @ followed by # with a line feed. The parenthesis around the first @ is a backreference ($1) that can be replaced with the tab that you want.
HTH,
^ _ ^
Copy link to clipboard
Copied
always impressive on regex as usual...
Copy link to clipboard
Copied
Merci, Birnou.
V/r,
^ _ ^
Copy link to clipboard
Copied
Actually, I did not quite think my solution all the way through. The backreference should not be around the @. There should be two of them around everything else, and manually insert the tab.
I will play with it some more, and post my final suggestion.
V/r,
^ _ ^
Copy link to clipboard
Copied
Okay, I've got it. But I also just discovered a bug in how Dw implements the replace.
IF YOU USE REPLACE ALL, IT WORKS. If you hit 'Replace' for just a single instance and do it repeatedly, it will skip lines. ???
Anyhoo.. my final solution:
Edward@C.@Sellner#
James@J.@Megivern#
J.@Philip@Newell#
FIND using REGEX: ([^@]{1,})(@)([^@]{1,})(@)([^#]{1,}#)
REPLACE: $1$2$3 $5 (This is with four spaces, as a tab cannot be used as part of the replace.)
REPLACE ALL command:
Edward@C. Sellner#
James@J. Megivern#
J.@Philip Newell#
REPLACE one at a time:
Edward@C. Sellner#
James@J.@Megivern#
J.@Philip Newell#
HTH,
^ _ ^
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Thanks, WolfShade! Great help!
Copy link to clipboard
Copied
You're welcome. And thank you for marking my answer as correct. I do appreciate it.
V/r,
^ _ ^