Match not limited by ^ and $ (newby to RegEx)

A swapping-ground for Regular Expression syntax

Match not limited by ^ and $ (newby to RegEx)

Postby TheGhost78 » Mon Jul 29, 2024 5:50 am

AAAA, BBBB and CCCC could all be any characters - alphanumeric or symbols.

I have this regular expression for swapping portions of a filename around:

Filename: AAAA - BBBB
Match: ^(.*) - (.*)$
Replace: \2 - \1
Resulting Filename: BBBB - AAAA

However, I have thousands of files and would like to be able to select them all in one go and have this regular expression ONLY affect the ones that are formatted as AAAA - BBBB. Some files are formatted as AAAA - BBBB - CCCC and are still affected by the regular expression despite the use of ^ and $ to mark the start and end. The files with CCCC are affected like this:

Filename: AAAA - BBBB - CCCC
Match: ^(.*) - (.*)$
Replace: \2 - \1
Resulting Filename: CCCC - AAAA - BBBB

How do I get the regular expression to only apply to the filenames formatted as AAAA - BBBB even if some selected files are formatted as AAAA - BBBB - CCCC?
TheGhost78
 
Posts: 173
Joined: Fri Jul 19, 2024 11:25 am

Re: Match not limited by ^ and $ (newby to RegEx)

Postby Admin » Mon Jul 29, 2024 6:43 am

Hi, can use Filters (12) -> Mask as a RegEx to filter only the format you need.
Admin
Site Admin
 
Posts: 2883
Joined: Tue Mar 08, 2005 8:39 pm

Re: Match not limited by ^ and $ (newby to RegEx)

Postby TheGhost78 » Mon Jul 29, 2024 11:23 am

Putting this ^(.+) - (.+)$ in the filter still brings up files formatted as AAAA - BBBB - CCCC. What should the filter be?
TheGhost78
 
Posts: 173
Joined: Fri Jul 19, 2024 11:25 am

Re: Match not limited by ^ and $ (newby to RegEx)

Postby TheGhost78 » Mon Jul 29, 2024 11:24 am

Same with this ^(.*) - (.*)$.
TheGhost78
 
Posts: 173
Joined: Fri Jul 19, 2024 11:25 am

Group (.*) matches too many characters

Postby Luuk » Mon Jul 29, 2024 1:52 pm

By far, the easiest way would be to just conduct a Filter(12)'s "Mask" like... "* - *" !"* - * - *"
It wont need the RegEx checkmark, and only shows names with exactly 1 text-string like ' - '

Inside of the RegEx(1) Match, there's nothing wrong with how ^ and $ are conducting...
The problem is that nothing tells either of your (.*) to not match strings with... ' - '
So a better Match to look something more like... ^(?!.* - .* - .*$)(.*) - (.*)$

But this new Match will only fix who gets renamed, not which names are inside of your file-listing!
Could also put this inside of the Filters(12) "Mask" (but remember it needs the checkmark for RegEx).
Luuk
 
Posts: 803
Joined: Fri Feb 21, 2020 10:58 pm

Re: Match not limited by ^ and $ (newby to RegEx)

Postby TheGhost78 » Mon Jul 29, 2024 8:16 pm

Thanks, guys!
TheGhost78
 
Posts: 173
Joined: Fri Jul 19, 2024 11:25 am


Return to Regular Expressions


cron