Page 1 of 1

Replacing hyphen after second \

Posted: Sat Jun 13, 2015 1:31 pm
by terrypin
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

Posted: Sat Jun 13, 2015 10:49 pm
by MudGuard
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.

Posted: Sun Jun 14, 2015 9:35 am
by ben_josephs
MudGuard: I believe Terry wants to replace all hyphens anywhere to the right of the second backslash on each line.

This will do that:
Find what: (^(?:[^\\\n]*\\){2}.*?|\G.*?)-
Replace with: $1

Posted: Sun Jun 14, 2015 10:44 am
by terrypin
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

Posted: Sun Jun 14, 2015 9:45 pm
by ben_josephs
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.

Posted: Sun Jun 14, 2015 10:50 pm
by ak47wong
terrypin wrote:As it stands, TP is not finding that regular expression. Or, if I replace G by F, assuming that was a typo, I get
You won't find any typos in ben_josephs's regexps. He only posts solutions that are well thought out and tested.

Posted: Mon Jun 15, 2015 7:01 am
by ben_josephs
Well, I do sometimes correct them or simplify them or improve them,

But thank you. :-)

Posted: Mon Jun 15, 2015 8:19 am
by terrypin
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.
Understood. That was my revised assumption as reported in my edit after more reading - clearly a more realistic conclusion than my earlier guess! :)
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.
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.

--
Terry, East Grinstead, UK

Posted: Mon Jun 15, 2015 5:06 pm
by terrypin
Ben: Pleased to confirm that with 7.5 installed your RegEx works a treat, thank you! :)

--
Terry, East Grinstead, UK

Posted: Mon Jun 15, 2015 7:57 pm
by ben_josephs
Great!

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
The text matched by the entire regex is replaced with

Code: Select all

$1            captured text number 1
              (that is, everything that was matched apart from the hyphen at the end)
:-)

Posted: Mon Jun 15, 2015 9:17 pm
by terrypin
Thanks, appreciated. I''ll study over a large coffee tomorrow.

--
Terry, East Grinstead, UK