Replacing a single character at unknown position

A swapping-ground for Regular Expression syntax

Replacing a single character at unknown position

Postby aderalia » Thu Jan 12, 2017 6:02 pm

Hey,
Im new to RegEx so i dont realy know how to do anything.
I spend the last 2 days finding out how they work but it didnt work to well :)

I need to replace a "-" Symbol with a "_" Symbol in a file name that is something like this:

2016-07-23_(Textblock1)-(Textblock2)-(Textblock3)-(Textblockn)

and i need to turn it into this:

2016-07-23_(Textblock1)-(Textblock2)_(Textblockn-1)-(Textblockn)

Every Textblock is made out of charakter (Capitals for every first charakter) and some but not all contain numbers, some are numbers only So every Textblock should be something like ([+A-Z][*a-z][*0-9])

Or in other words: replace the secound - from behind with a _

In my understanding it contains something like $[-], but i dont know how to tell it that i want the secound one.
Also [_] as replace action didnt work...

If someone could give me the required code and maybe one or two lines of explenation would be awesome.
I just cant wrap my head arround RegEx...
Thanks!
aderalia
 
Posts: 2
Joined: Thu Jan 12, 2017 5:45 pm

Re: Replacing a single character at unkown possition

Postby KenP » Thu Jan 12, 2017 9:10 pm

If I'm understanding correctly you want to replace the last but one hyphen with an underscore, if that's correct this should work.

RegEx (1)
Match: ^(.*)-(.*-.*)$
Replace: \1_\2



If you want to do more than change the last but one hyphen to an underscore let us know and we'll help :wink:
KenP
 
Posts: 199
Joined: Sat Jul 30, 2016 11:25 am

Re: Replacing a single character at unkown possition

Postby aderalia » Thu Jan 12, 2017 10:42 pm

Yep worked perfectly :)
Thanks a lot!

I found out that some of the Foldernames dont follow the sceme i had asumed so i had to modify it a bit for these ones (i just sort them with the Filter Option)
aderalia
 
Posts: 2
Joined: Thu Jan 12, 2017 5:45 pm


Return to Regular Expressions