Page 1 of 1

[SOLVED] How formatlist() filters item containing characters such as []

Posted: 29 Apr 2024 23:23
by Norn
Use a list like this for testing:
list.png
Items containing [] characters will not be filtered by formatlist():
result.png

Code: Select all

    $items = get("ItemsPathNames", "<crlf>", "a");
    $paper = "";
    foreach ($item, $items, <crlf>) {
        if (!regexmatches($item, "^.*zip$|^.*rar$|^.*7z$")) {continue}
        $text = regexreplace($item, "(^.*).zip|.rar|.7z$", "$1");
        $text = regexreplace($text, "(^.*)\([0-9]{1,3}\)", "$1");
        $match = formatlist($items, "f", <crlf>, "$text<crlf>$text*");
        if (gettoken($match, 2, <crlf>)) {$paper .= $match . <crlf>}
    }
    $paper = formatlist($paper, "sd", <crlf>);
    paperfolder("Similar item name", $paper, <crlf>, "nl");
    //goto(lax('vi:$paper //"Similar item name"'));
    sortbylist $paper
Or is there any other simple way?

Re: How formatlist() filters item containing characters such as []

Posted: 30 Apr 2024 00:10
by highend
formatlist treats [ as the beginning of a range and # as a digit, so you need to escape them by surrounding them with []

Sorry but what is the exact expected output?
The result.png can't be it,
if (!regexmatches($item, "^.*zip$|^.*rar$|^.*7z$")) { continue; }
would filter everything out but:

Code: Select all

[2].zip
[2](1).zip
1.zip
And in general, you should do the prefiltering before the loop, e.g.:

Code: Select all

    $items = <get SelectedItemsPathNames>;
    $items = regexmatches($items, "^.*?(zip|rar|7z)$", <crlf>);
Btw:

Code: Select all

if (!regexmatches($item, "^.*zip$|^.*rar$|^.*7z$")) {continue} => "^.*\.(zip|rar|7z)$"
$text = regexreplace($item, "(^.*).zip|.rar|.7z$", "$1"); => Your dots before the extensions aren't really dots here, they are metachars
"(^.*)\([0-9]{1,3}\)" => Be very careful with such greedy expressions, the * would eat anything, make them lazy instead

Re: How formatlist() filters item containing characters such as []

Posted: 30 Apr 2024 09:48
by Norn
formatlist treats [ as the beginning of a range and # as a digit, so you need to escape them by surrounding them with []
I have tried many times but no luck.
I haven't formally learned regular expressions yet, and you can see the mess of expressions in my code...

Re: How formatlist() filters item containing characters such as []

Posted: 30 Apr 2024 10:00
by highend
$text = regexreplace($text, "(\[|#)", "[$1]");

Re: How formatlist() filters item containing characters such as []

Posted: 30 Apr 2024 10:20
by Norn
Thanks!
I tested by renaming and it seems to be ineffective. :oops:
items.png

Re: [SOLVED] How formatlist() filters item containing characters such as []

Posted: 30 Apr 2024 10:28
by highend
What? This is for filtering items with formatlist(), not renaming them...

Again: For the files / folders you've listed, what should your script (the paperfolder) output / contain at the end?