Replace all spaces and "_" with "-" but spaces and "_" th...

A swapping-ground for Regular Expression syntax

Replace all spaces and "_" with "-" but spaces and "_" th...

Postby rvindell » Fri Aug 15, 2014 10:27 pm

I want to replace all spaces and "_" with "-"

If space is before or after a "-" sign i want just to remove the spaces

If space is before or after a "_" sign i want it to remove spacer and replace "_" with "-"

Example of file:

this - is _ my-file.jpg

would become

this-is-my-file.jpg

Any brilliant person able to come up with a solution for this? I've been trying forever but i can't get it right.

I'm using: regex > match: (.*)( - )(.*) > replace: \1-\3

which seems to remove the issue of having spacer - spacer and replacing that with only "-"

But the rest i can't get to work :)
rvindell
 
Posts: 4
Joined: Fri Aug 15, 2014 10:18 pm

Re: Replace all spaces and "_" with "-" but spaces and "_" th...

Postby Stefan » Sat Aug 16, 2014 9:23 am

 

FROM:
1this - is _ my-file.jpg
2Test here_is_space right.ext
3Test here_is_space - right.ext
4Test here _ is_space right.ext

TO:
1this-is-my-file.jpg
2Test-here-is-space-right.ext
3Test-here-is-space-right.ext
4Test-here-is-space-right.ext


DO:


Just in case, be sure to check "Options Menu > Prevent Duplicates"
Options Menu > Prevent Duplicates wrote:Prevent Duplicates

This option allows to you to overcome the situation whereby a rename would fail because a file with the same name already exists.
If you try to rename a file, and there's already a file with the same name, the software will make up subsequent attempt to rename the file but with a "_1" suffix.
If this fails it will try with "_2" as the suffix, and will continue up to "_99". The limit of 99, and the separator character (underscore, _) are currently fixed and cannot be changed.




--------------- First step:
Repl(3)
Replace: underscore
With: space

Remove(5)
[x]D/S

[Rename]



--------------- Second step:

Repl(3)
Replace: hyphen
With: space

Remove(5)
[x]D/S

[Rename]



--------------- Third step:

Repl(3)
Replace: space
With: hyphen

[Rename]

--------------- Done!


###################################################


FROM:
1this - is _ my-file.jpg
2Test here_is_space right.ext
3Test here_is_space - right.ext
4Test here _ is_space right.ext

TO:
1this-is-my-file.jpg
2Test here-is-space right.ext
3Test here-is-space-right.ext
4Test here-is-space right.ext

RULE:
Prevent space between words.



--------------- First step:
Repl(3)
Replace: space
With: #

Remove(5)
[x]D/S

[Rename]



--------------- Second step:
Repl(3)
Replace: underscore
With: space

Remove(5)
[x]D/S

[Rename]



--------------- Third step:
Repl(3)
Replace: hyphen
With: space

Remove(5)
[x]D/S

[Rename]



--------------- Fourth step:
Repl(3)
Replace: space
With: hyphen

[Rename]


--------------- Fifth step:
Repl(3)
Replace: #
With: space

Remove(5)
[x]D/S

[Rename]

--------------- Sixth step:

Repl(3)
Replace: space-hyphen-space
With: hyphen

[Rename]


--------------- Done!



 
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Re: Replace all spaces and "_" with "-" but spaces and "_" th...

Postby rvindell » Sat Aug 16, 2014 1:46 pm

Thanks Stefan, but i would like to accomplish the entire thing with 1 step.

I've gotten closer with your tips, i've setup like this:

Regex (1)
Match: (.*)( - )(.*)
Replace: \1-\3

Repl. (3)
Replace: _
With: spacer(actual space)

Remove (5)
[X] D/S

This puts me at:

Original filename: this - is _ my-file.ext

Renamed filename: this-is my-file.ext

Now if i replace spacer with - i have achieved my goal, but to do all this in 1 click, no clue :)
rvindell
 
Posts: 4
Joined: Fri Aug 15, 2014 10:18 pm

Re: Replace all spaces and "_" with "-" but spaces and "_" th...

Postby Stefan » Sat Aug 16, 2014 3:43 pm

FROM:
this - is _ my-file.ext
TO:
this-is my-file.ext

DO:
Regex (1)
Match: (.*)( - )(.*)
Replace: \1-\3
+
Repl. (3)
Replace:space-underscore-space
With: hyphen
[Rename]

Done!
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU


Return to Regular Expressions