Insert a 'dash' after second word

A swapping-ground for Regular Expression syntax

Insert a 'dash' after second word

Postby RobsterUK » Fri Jan 22, 2016 11:42 am

Hi All,
I'm back again.
I can't get enough of this brilliant utility.
It's making me have an OCD about my filenames :D

I'm trying to insert a dash "-" after the second word in a file.
Nothing is similar in each file, ie length or specific characters or digits, so each file is unique apart from wanting a dash after the second word.

Examples:
From:
My Text Document has no Dash.txt
My Document has no Dash.txt

To:
My Text - Document has no Dash.txt
My Document - has no Dash.txt

I've tried
RegEx(1)
Find
^(.+\s)(.+\s)(.+)$
Replace
\1\2- \3

But that results in:
My Text Document has no - Dash.txt
My Document has no - Dash.txt

With the dash before the last word
Could someone point me in the right direction. I'd be very grateful :)
RobsterUK
 
Posts: 8
Joined: Wed Jan 20, 2016 12:00 pm

Re: Insert a 'dash' after second word

Postby therube » Fri Jan 22, 2016 9:14 pm

find
(\w+) (\w+) (\w+.*)

replace
\1 \2 - \3


For some reason, a filename like:

10 C-Boy's Blues.mp3

fails, & I'm not sure why offhand?
therube
 
Posts: 1360
Joined: Mon Jan 18, 2016 6:23 pm

Re: Insert a 'dash' after second word

Postby RobsterUK » Sat Jan 23, 2016 10:24 am

Thanks for the code.

I guess the \w matches a word. I haven't seen this documented anywhere, which would have helped.

Maybe the reason it doesn't work for
10 C-Boy's Blues.mp3
is because it starts with digits, then the word 'C-Boy's' is more complex. ie it has a dash and a comma, so it may be too complex for the search criteria.

Anyway thanks for the \w switch. If i had known about that one, then I probably could have figured this out!
RobsterUK
 
Posts: 8
Joined: Wed Jan 20, 2016 12:00 pm


Return to Regular Expressions


cron