Replace only the first occurrence of a string

A swapping-ground for Regular Expression syntax

Replace only the first occurrence of a string

Postby edr » Tue Jul 04, 2017 6:30 pm

Hi Guys,

Apologies if this has been answered, but I have searched the forum for a solution many times on separate days and looked at the help section on regular expressions for something similar to what I want to do but have not found anything yet. I'm sure that if someone can please help me with a Match/Replace RegEx (1) that works it will finally make sense to me.

What I want to do is change the file names so that the first two digits of the file name are retained in positions 1-2, replace the pattern of " - " in positions 3-5 only with "-APPLE01-" and replace the other occurrences of " - " in the remaining character positions of the file name with "-" no matter how many times it is repeated.

For Example:
01 - This is - the first file name.txt
02 - This is the - second - file - name.txt
03 - This the third file name.txt

Would be changed to:
01-APPLE01-This is-the first file name.txt
02-APPLE01-This is the-second-file-name.txt
03-APPLE01-This is the third file name.txt

Is there a regular expression that will do this in one sweep to a group of selected file names in a folder? I tried using the Replace (3) Replace/With function but it replaces all the " - " strings in the file name. For example, my file names would be changed to:

01-APPLE01-This is-APPLE01-the first file name.txt
02-APPLE01-This is the-APPLE01-second-APPLE01-file-APPLE01-name.txt

Thanks!
Ed
edr
 
Posts: 2
Joined: Tue Jul 04, 2017 6:12 pm

Re: Replace only the first occurrence of a string

Postby edr » Tue Jul 04, 2017 6:55 pm

Ah, I figured something out by using the Replace (3) and Add (7) functions.

In Replace (3), I put Replace " - " With "-"

In Add (7), I Insert "-APPLE01" at pos. 3

That works!

Don't know why I didn't think of doing this weeks ago!

Cheers and Happy 4th of July for those of you in the United States!
Ed
edr
 
Posts: 2
Joined: Tue Jul 04, 2017 6:12 pm


Return to Regular Expressions