Omit previously changed filenames

A swapping-ground for Regular Expression syntax

Omit previously changed filenames

Postby CharlesFromVA » Sat Nov 08, 2014 2:29 am

I've run the following to add a space before an ending digit for a filename; for example:

Variable Number of Words Filename1 becomes Variable Number of Words Filename 1

RegEx(1):
Match
Code: Select all
(.+)(\d)$

Replace
Code: Select all
\1 \2

Now I want to run it on files that have since been added; however, it's adding another space before digits that already have a space before them, resulting in two spaces before the digit. In other words, I want to exclude previously-changed filenames from the Match. I tried the following, which successfully ignored the previously re-named files:
Match
Code: Select all
(.+)[^\s](\d)$

Replace
Code: Select all
\1 \2

However, it substituted a space for the character immediately preceding the digit, rather than inserting a space:

Variable Number of Words Filename1 became Variable Number of Words Filenam 1

I've fiddled for an hour; I give up. I would greatly appreciate any help that's available.

Thanks!
CharlesFromVA
 
Posts: 4
Joined: Sat Nov 08, 2014 12:17 am

Re: Omit previously changed filenames

Postby CharlesFromVA » Sat Nov 08, 2014 2:48 am

Okay, I got it to work, but I don't understand why it does. Here's what works:

RegEx (1)
Match:
Code: Select all
(.+)([^\s])(\d)$

Replace:
Code: Select all
\1\2 \3

If someone could explain why it works, I'd appreciate it. Is it treating ([^\s]) as any non-space character that immediately precedes the digit? That's all I can think of, but this stuff is really confusing to me.

Thanks for your patience and assistance!
CharlesFromVA
 
Posts: 4
Joined: Sat Nov 08, 2014 12:17 am

Re: Omit previously changed filenames

Postby TheRegExpMaster » Mon Dec 15, 2014 1:47 pm

Hi, yes you asked it to match a string that doesn't end with a space, and is immediately followed by a digit.

You can also use this regex that has the advantage of replacing zero or more spaces by just 1 space.

MATCH :
Code: Select all
(.+?)\s*(\d)$

REPLACE :
Code: Select all
\1 \2

".+?" is the non-greedy matching that allows "\s*" to match as many trailing spaces as possible before the digit :

Variable Number of Words Filename1 becomes Variable Number of Words Filename 1
Variable Number of Words Filename 1 becomes Variable Number of Words Filename 1 (no matter how many spaces are between "Filename" and "1".
TheRegExpMaster
 
Posts: 25
Joined: Wed Nov 05, 2014 8:45 pm

Re: Omit previously changed filenames

Postby CharlesFromVA » Mon Dec 15, 2014 2:32 pm

Thank you! I used to think I was fairly astute when it came to search and replace, but regex is a whole different ballgame that I'm really having a hard time wrapping my head around. I really appreciate the help.
CharlesFromVA
 
Posts: 4
Joined: Sat Nov 08, 2014 12:17 am

Re: Omit previously changed filenames

Postby TheRegExpMaster » Mon Dec 15, 2014 9:23 pm

CharlesFromVA wrote:Thank you! I used to think I was fairly astute when it came to search and replace, but regex is a whole different ballgame that I'm really having a hard time wrapping my head around. I really appreciate the help.

You're welcome :P Regex look complicated for anybody at the beginning, but once you understand how it works, it's an amazing timesaver. I learnt it 15 years ago when programming Perl. What is the most useful for me is the "non-greedy matching" (adding the question mark after the quantifier) because the greediness can be often out of control especially when you use ".+".
TheRegExpMaster
 
Posts: 25
Joined: Wed Nov 05, 2014 8:45 pm

Re: Omit previously changed filenames

Postby ConnieKeefe » Tue Dec 30, 2014 9:56 am

This RegEx only works if the folder ends with "(some number of 4 digits) random number of space [any caracter excluding closing brackets]"

For your information :
\1 stores everything before the year
\2 stores the opening parenthesis before the year
\3 stores the year
\4 stores everything after the year
640-760 vce
Please tell me if it works ;)
ConnieKeefe
 
Posts: 1
Joined: Tue Dec 30, 2014 9:38 am

Re: Omit previously changed filenames

Postby TheRegExpMaster » Tue Dec 30, 2014 6:01 pm

ConnieKeefe wrote:This RegEx only works if the folder ends with "(some number of 4 digits) random number of space [any caracter excluding closing brackets]"

For your information :
\1 stores everything before the year
\2 stores the opening parenthesis before the year
\3 stores the year
\4 stores everything after the year
640-760 vce
Please tell me if it works ;)

err I think you are replying in the wrong topic, Connie ;)
TheRegExpMaster
 
Posts: 25
Joined: Wed Nov 05, 2014 8:45 pm


Return to Regular Expressions