newbie regexp for this task - long email filenames?

A swapping-ground for Regular Expression syntax

newbie regexp for this task - long email filenames?

Postby bkuser » Sun Aug 19, 2012 11:31 am

Hi,
I have a folder with many files that start with an email address eg:

"joe@here.com - rest of file name includes spaces - main-name is here.txt" (no quotes surround the filname)
(yes I know it's a craze list of names).

Anyway, I'd like to remove the prefix text up the word "main" as that where the files begin to differ..

I thought regex would be good for this so in search I put all the text up to the space just before the world "main" and left replacement text filed empty. My hope was all that leading text would be stripped. However all I ever see as the final result is the .txt (ie the extension)

There is probably an easier way to strip that text tha using regex, (by position maybe - butthen I'd have to count all those characters), but appreciate knowing where I have gone wrong.
thanks
bkuser
 
Posts: 2
Joined: Sun Aug 19, 2012 11:22 am

Re: newbie regexp for this task - long email filenames?

Postby Stefan » Sun Aug 19, 2012 6:21 pm

FROM:
joe@here.com - rest of file name includes spaces - main-name is here.txt
TO:
main-name is here.txt"

Rule:
eat all to the last dash/space combination and store that in ()-group \1
eat all the following rest and store that in ()-group \2

Use
RegEx:
Match: (.+- )(.+)
Repla: \2


Note:
to store the first match in group \1 is not really necessary but chosen here for clarification.
So this will also work:
Match: .+- (.+)
Repla: \1


Explanation:
. > match one of any sign
+ > match one-or-more of the expression right before (any sign here)
- > an dash literary.
' '> match an space
So: match one-or-more of any sign till an dash following by an space (match greedy)




If one would want to match till the first dash only,
"rest of file name includes spaces - main-name is here.txt"

we need to match NON-greedy by using an '?'
Match: .+?- (.+)
Repla: \1


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

Re: newbie regexp for this task - long email filenames?

Postby bkuser » Mon Aug 20, 2012 11:41 am

Many thanks Stefan - worked perfectly. And I have learn more how to start using regexp.
bkuser
 
Posts: 2
Joined: Sun Aug 19, 2012 11:22 am


Return to Regular Expressions