Replacing hyphen after second \
Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard
Replacing hyphen after second \
I'm struggling a bit with this one so any help would be appreciated please.
I want to replace all hyphens after the second \ in a string.
So for example
F:\abc-def\123-456\-xyz-789
would become
F:\abc-def\123456\xyz789
If it helps, the 2nd and 3rd characters will always be :\
I suspect it would need two runs?
Terry, East Grinstead, UK
I want to replace all hyphens after the second \ in a string.
So for example
F:\abc-def\123-456\-xyz-789
would become
F:\abc-def\123456\xyz789
If it helps, the 2nd and 3rd characters will always be :\
I suspect it would need two runs?
Terry, East Grinstead, UK
Your example does not match your description - you say you want to remove a hyphen after the second backslash, but your example shows removing of a hyphen after the third backslash ...
My guess would be: after the last slash.
If so, search for \\-([^\\]*?)$ and replace by \\$1
If it is the second \, search for ([^\\]*\\[^ \\]*\\)- and replace by $1.
If it is the third \, search for ([^\\]*\\[^ \\]*\\[^ \\]*\\)- and replace by $1.
My guess would be: after the last slash.
If so, search for \\-([^\\]*?)$ and replace by \\$1
If it is the second \, search for ([^\\]*\\[^ \\]*\\)- and replace by $1.
If it is the third \, search for ([^\\]*\\[^ \\]*\\[^ \\]*\\)- and replace by $1.
-
ben_josephs
- Posts: 2464
- Joined: Sun Mar 02, 2003 9:22 pm
Many thanks, Ben!
But could you clarify that 'G' please? And how does TextPad interpret that $1?
As it stands, TP is not finding that regular expression. Or, if I replace G by F, assuming that was a typo, I get
1789
instead of
F:\abc-def\123456\xyz789
It's clearly down to my poor know-how and I also suspect I have my aging 4.7.3 version of TP configured significantly differently in some key respect.
--------------------
Mudguard:
https://dl.dropboxusercontent.com/u/401 ... hens-1.jpg
This is the result I get, which reinforces my conclusion that it all hinges on that $ 'operator', which I understood to mean 'end of line'. Could you or Ben step me through it when you get a few minutes please?
F:\$1xyz-789
--------------------
EDIT: After further research, is the issue simply that I'm using the POSIX version of RegEx and you're both using the PERL version?
--
Terry, East Grinstead, UK
But could you clarify that 'G' please? And how does TextPad interpret that $1?
As it stands, TP is not finding that regular expression. Or, if I replace G by F, assuming that was a typo, I get
1789
instead of
F:\abc-def\123456\xyz789
It's clearly down to my poor know-how and I also suspect I have my aging 4.7.3 version of TP configured significantly differently in some key respect.
--------------------
Mudguard:
https://dl.dropboxusercontent.com/u/401 ... hens-1.jpg
This is the result I get, which reinforces my conclusion that it all hinges on that $ 'operator', which I understood to mean 'end of line'. Could you or Ben step me through it when you get a few minutes please?
F:\$1xyz-789
--------------------
EDIT: After further research, is the issue simply that I'm using the POSIX version of RegEx and you're both using the PERL version?
--
Terry, East Grinstead, UK
-
ben_josephs
- Posts: 2464
- Joined: Sun Mar 02, 2003 9:22 pm
There are no typos in the solution I offered, but it will not work in versions of TextPad earlier than version 7.
Your version is eleven years old and three full versions behind the current one. Its regex support is very different from and very much weaker than the current one's. There is no way to do anything remotely like my suggestion in your version. Unless you upgrade you will have to do it in stages.
Your version is eleven years old and three full versions behind the current one. Its regex support is very different from and very much weaker than the current one's. There is no way to do anything remotely like my suggestion in your version. Unless you upgrade you will have to do it in stages.
-
ben_josephs
- Posts: 2464
- Joined: Sun Mar 02, 2003 9:22 pm
Understood. That was my revised assumption as reported in my edit after more reading - clearly a more realistic conclusion than my earlier guess!ben_josephs wrote:There are no typos in the solution I offered, but it will not work in versions of TextPad earlier than version 7.
I've put it off for years but I'm updating right now. Having trouble recovering my settings and I'll post in the General forum.Your version is eleven years old and three full versions behind the current one. Its regex support is very different from and very much weaker than the current one's. There is no way to do anything remotely like my suggestion in your version. Unless you upgrade you will have to do it in stages.
--
Terry, East Grinstead, UK
-
ben_josephs
- Posts: 2464
- Joined: Sun Mar 02, 2003 9:22 pm
Great!
And this is how it works...
(^(?:[^\\\n]*\\){2}.*?|\G.*?)- matches
The text matched by the entire regex is replaced with

And this is how it works...
(^(?:[^\\\n]*\\){2}.*?|\G.*?)- matches
Code: Select all
( (start of captured text number 1)
either:
^ the beginning of a line
(?: (start of non-capturing group)
[^\\\n]* any (possibly empty) string not containing a backslash or newline
\\ a backslash
) (end of non-capturing group)
{2} ... twice
.*? the shortest (possibly empty) string not containing a newline that allows the whole expression to match
| or
\G (only at the end of the previous match or at the start of the text)
.*? the shortest (possibly empty) string not containing a newline that allows the whole expression to match
) (end of captured text number 1)
- a hyphen
Code: Select all
$1 captured text number 1
(that is, everything that was matched apart from the hyphen at the end)