Similar filename matches in a directory

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
hermhart
Posts: 136
Joined: 13 Jan 2015 18:41

Re: Similar filename matches in a directory

Post by hermhart » 25 Mar 2019 22:23

Well, I can't thank you enough for taking a look at it for me. I appreciate it.

Would you be able to explain these lines to me though? I just want to be able to understand and learn from them. I understand the setting the variable part of the lines, and using the regexreplace, but I'm just not too good with the regular expressions yet to understand these clearly and don't also understand what the \ in front of the replacement part is (\$1 or the \$2).

Code: Select all

        $escaped = regexreplace($id, "([\\.+(){\[^$])", "\$1");

        $matches = regexmatches($files, "^" . $escaped . ".*?(?=\r?\n|$)", <crlf>, 1);

                $second = regexreplace($match, "^(.*?)~([^.]+)(.*)", "$2");
                $secondEscaped = regexreplace($second, "([\\.+(){\[^$])", "\$1");

        $files = formatlist(regexreplace($files, "^" . $escaped . ".*?(?=\r?\n|$)", , 1), "e", <crlf>);
 

highend
Posts: 8304
Joined: 06 Feb 2011 00:33

Re: Similar filename matches in a directory

Post by highend » 25 Mar 2019 22:48

$escaped = regexreplace($id, "([\\.+(){\[^$])", "\$1");
It is escaping (adding a backspace in front of every char that is inside the group).
Why? Because using regexmatches / regexreplace requires escaped metacharacters

$matches = regexmatches($files, "^" . $escaped . ".*?(?=\r?\n|$)", <crlf>, 1);
Looking for groups here (all files that belong to the same group). A line ending is not captured

$second = regexreplace($match, "^(.*?)~([^.]+)(.*)", "$2");
Getting the group after the ~ char

$secondEscaped = regexreplace($second, "([\\.+(){\[^$])", "\$1");
Again, escaping it.

$files = formatlist(regexreplace($files, "^" . $escaped . ".*?(?=\r?\n|$)", , 1), "e", <crlf>);
Removing all files from the current group from $files. Could be done without the formatlist
but I was too lazy.

Note: Escaping metachars isn't a necessity for your special case because atm non of the example
file (base!) names contains a dot, square bracket or any other of the other metachars but I don't rely
on a few examples and think: better safe than sorry. Your original regex states that e.g. the first group
could also contain a dot and that would need to be escaped to be able to be used in a regexmatches / regexreplace!
One of my scripts helped you out? Please donate via Paypal or highend (at) web (dot) de

hermhart
Posts: 136
Joined: 13 Jan 2015 18:41

Re: Similar filename matches in a directory

Post by hermhart » 26 Mar 2019 13:30

Thank you for the explanations, highend. :appl:

Post Reply