Replacing hyphen after second \

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
terrypin
Posts: 174
Joined: Wed Jul 11, 2007 7:50 am

Replacing hyphen after second \

Post 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
User avatar
MudGuard
Posts: 1295
Joined: Sun Mar 02, 2003 10:15 pm
Location: Munich, Germany
Contact:

Post 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.
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post 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
terrypin
Posts: 174
Joined: Wed Jul 11, 2007 7:50 am

Post 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
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post 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.
ak47wong
Posts: 703
Joined: Tue Aug 12, 2003 9:37 am
Location: Sydney, Australia

Post 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.
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Well, I do sometimes correct them or simplify them or improve them,

But thank you. :-)
terrypin
Posts: 174
Joined: Wed Jul 11, 2007 7:50 am

Post 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
terrypin
Posts: 174
Joined: Wed Jul 11, 2007 7:50 am

Post by terrypin »

Ben: Pleased to confirm that with 7.5 installed your RegEx works a treat, thank you! :)

--
Terry, East Grinstead, UK
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post 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)
:-)
terrypin
Posts: 174
Joined: Wed Jul 11, 2007 7:50 am

Post by terrypin »

Thanks, appreciated. I''ll study over a large coffee tomorrow.

--
Terry, East Grinstead, UK
Post Reply