Regex and () in directory name

Discussion related to "Everything" 1.5 Alpha.
Post Reply
anmac1789
Posts: 673
Joined: Mon Aug 24, 2020 1:16 pm

Regex and () in directory name

Post by anmac1789 »

How to use Regex with brackets in folder names ? When finding duplicates using folder name brackets, Regex rejects the folder.
therube
Posts: 5003
Joined: Thu Sep 03, 2009 6:48 pm

Re: Regex and () in directory name

Post by therube »

regex:"C:\\TMP\\BRU\\7eventy 5ive \(2007\)"

regex:"C:\\TMP\\BRU\\7eventy 5ive \[2007\] - Copy"
anmac1789
Posts: 673
Joined: Mon Aug 24, 2020 1:16 pm

Re: Regex and () in directory name

Post by anmac1789 »

therube wrote: Tue Apr 11, 2023 8:40 pm
regex:"C:\\TMP\\BRU\\7eventy 5ive \(2007\)"

regex:"C:\\TMP\\BRU\\7eventy 5ive \[2007\] - Copy"
Does \\ need to be added at the end of the path string ?
anmac1789
Posts: 673
Joined: Mon Aug 24, 2020 1:16 pm

Re: Regex and () in directory name

Post by anmac1789 »

What if there is a subfolder inside the path with ( ) ?
void
Developer
Posts: 16897
Joined: Fri Oct 16, 2009 11:31 pm

Re: Regex and () in directory name

Post by void »

Use ^ to match the start of the filename and improve regex performance:

regex:"^C:\\TMP\\BRU\\7eventy 5ive \(2007\)"
regex:"^C:\\TMP\\BRU\\7eventy 5ive \[2007\] - Copy"



Use a trailing $ to match the end of the filename:

regex:"^C:\\TMP\\BRU\\7eventy 5ive \(2007\)$"
regex:"^C:\\TMP\\BRU\\7eventy 5ive \[2007\] - Copy$"

-or-

Use a trailing \\ to match anything under the specified path

regex:"^C:\\TMP\\BRU\\7eventy 5ive \(2007\)\\"
regex:"^C:\\TMP\\BRU\\7eventy 5ive \[2007\] - Copy\\"

without the trailing \\, these searches will match:

C:\TMP\BRU\7eventy 5ive (2007) Backup"
C:\TMP\BRU\7eventy 5ive [2007] - Copy - Copy (2)"



If you are trying to capture the name in the specific path, use:

regex:"^C:\\TMP\\BRU\\7eventy 5ive \(2007\)\\([^\\]*$)"
regex:"^C:\\TMP\\BRU\\7eventy 5ive \[2007\] - Copy\\([^\\]*$)"

( ) = capture group.
[^\\] = match any character except \\
* = match previous element zero or more times.



Use \( or \) to match a literal ( or )
[^\\] will also match ( or )
anmac1789
Posts: 673
Joined: Mon Aug 24, 2020 1:16 pm

Re: Regex and () in directory name

Post by anmac1789 »

Could you give an example with results of

1. Use a trailing $ to match the end of the filename:
2. Use a trailing \\ to match anything under the specified path and without trailing \\
3. Using \\([^\\]*$)"
horst.epp
Posts: 1453
Joined: Fri Apr 04, 2014 3:24 pm

Re: Regex and () in directory name

Post by horst.epp »

anmac1789 wrote: Wed Apr 12, 2023 1:10 pm Could you give an example with results of

1. Use a trailing $ to match the end of the filename:
2. Use a trailing \\ to match anything under the specified path and without trailing \\
3. Using \\([^\\]*$)"
Why not looking on the examples Void gave you one post above your question.
void
Developer
Posts: 16897
Joined: Fri Oct 16, 2009 11:31 pm

Re: Regex and () in directory name

Post by void »

Please try adding the Regular Expression Match 1 column to see what is being captured.

Experiment with:

regex:^C:\\FolderA\\([^\\]*)$
regex:^C:\\FolderA\\(.*)$

Adjust C:\\FolderA as needed.



Typically ^ will go at the start of your regex search and $ will go at the end to match the whole path and filename.

$1: used with fileexists: is replaced for each file/folder with the value shown in the Regular Expression Match 1 column.

Perl Compatible Regular Expressions
anmac1789
Posts: 673
Joined: Mon Aug 24, 2020 1:16 pm

Re: Regex and () in directory name

Post by anmac1789 »

So it seems that this expression matches folders only while,
void wrote: Wed Apr 12, 2023 11:05 pm regex:^C:\\FolderA\\([^\\]*)$
this expression gives the folder and the file that it contains in the regular expression match 1 column
void wrote: Wed Apr 12, 2023 11:05 pm regex:^C:\\FolderA\\(.*)$
so the reg exp 1 column1 captures the filename and the subfolder but not just the filename itself...
NotNull
Posts: 5462
Joined: Wed May 24, 2017 9:22 pm

Re: Regex and () in directory name

Post by NotNull »

The first one should capture just the filename (without path)

The second one should capture the complete path of a file/folder, minus "C:\FolderA\"
anmac1789
Posts: 673
Joined: Mon Aug 24, 2020 1:16 pm

Re: Regex and () in directory name

Post by anmac1789 »

NotNull wrote: Thu Apr 13, 2023 7:22 pm The first one should capture just the filename (without path)

The second one should capture the complete path of a file/folder, minus "C:\FolderA\"
Let's say I wanted to match files from 2 folders in different subfolders at different levels ? and to further complicate things..what if a subfolder has ( ) in them ? will it automatically add \ to subfolder names with ( ) to different levels ?
NotNull
Posts: 5462
Joined: Wed May 24, 2017 9:22 pm

Re: Regex and () in directory name

Post by NotNull »

I haven't read this thread; just responded on your (then) last post.
anmac1789
Posts: 673
Joined: Mon Aug 24, 2020 1:16 pm

Re: Regex and () in directory name

Post by anmac1789 »

NotNull wrote: Thu Apr 13, 2023 7:31 pm I haven't read this thread; just responded on your (then) last post.
Ok...
void
Developer
Posts: 16897
Joined: Fri Oct 16, 2009 11:31 pm

Re: Regex and () in directory name

Post by void »

To match subfolders and files, please try the following:

<regex:^C:\\FolderA\\(.*)$ fileexists:C:\\FolderB\\\1> | <regex:^C:\\FolderB\\(.*)$ fileexists:C:\\FolderA\\\1>

(.*) will capture ( and ) without issues.
any ( or ) text replaced with \1 is handled without any issues.
anmac1789
Posts: 673
Joined: Mon Aug 24, 2020 1:16 pm

Re: Regex and () in directory name

Post by anmac1789 »

void wrote: Fri Apr 14, 2023 12:48 am To match subfolders and files, please try the following:

<regex:^C:\\FolderA\\(.*)$ fileexists:C:\\FolderB\\\1> | <regex:^C:\\FolderB\\(.*)$ fileexists:C:\\FolderA\\\1>

(.*) will capture ( and ) without issues.
any ( or ) text replaced with \1 is handled without any issues.
So, the brackets will be captured within subfolders too ?
void
Developer
Posts: 16897
Joined: Fri Oct 16, 2009 11:31 pm

Re: Regex and () in directory name

Post by void »

Yes, ( and ) will be captured with .*

Please make sure you escape any special regex characters in the path that you substitute with C:\\FolderA.

For example:

Instead of C:\FolderA you want to use C:\Program Files (x86)
use the following regex:
regex:"^C:\\Program files \(x86\)"

If unsure, use the following search preprocessor:

regex:^[regexescape:"C:\Program Files (x86)"]
anmac1789
Posts: 673
Joined: Mon Aug 24, 2020 1:16 pm

Re: Regex and () in directory name

Post by anmac1789 »

void wrote: Fri Apr 14, 2023 1:31 am Yes, ( and ) will be captured with .*

Please make sure you escape any special regex characters in the path that you substitute with C:\\FolderA.

For example:

Instead of C:\FolderA you want to use C:\Program Files (x86)
use the following regex:
regex:"^C:\\Program files \(x86\)"

If unsure, use the following search preprocessor:

regex:^[regexescape:"C:\Program Files (x86)"]
Thanks I will try this out
anmac1789
Posts: 673
Joined: Mon Aug 24, 2020 1:16 pm

Re: Regex and () in directory name

Post by anmac1789 »

void wrote: Fri Apr 14, 2023 12:48 am To match subfolders and files, please try the following:

<regex:^C:\\FolderA\\(.*)$ fileexists:C:\\FolderB\\\1> | <regex:^C:\\FolderB\\(.*)$ fileexists:C:\\FolderA\\\1>

(.*) will capture ( and ) without issues.
any ( or ) text replaced with \1 is handled without any issues.
Is this supposed to match files inside subfolders with other files in a directory in different level subfolders ? Here is a screenshot of
U:\regex test folder 1\
and
U:\regex test folder 1 (brackets)\
folder
Screenshot 2023-04-14 220906.jpg
Screenshot 2023-04-14 220906.jpg (79.96 KiB) Viewed 5794 times

When I use the syntax from the above quote into these 2 types of folders then the following results is produced:
<regex:^"U:\\regex test folder 1 (brackets)\\(.*)$" fileexists:"U:\\regex test folder 1\\\1"> | <regex:^"U:\\regex test folder 1\\(.*)$" fileexists:"U:\\regex test folder 1 (brackets)\\\1">

Screenshot 2023-04-14 222210.jpg
Screenshot 2023-04-14 222210.jpg (61.73 KiB) Viewed 5794 times
Post Reply