Regular Expression Help

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

Regular Expression Help

Post by hermhart »

Hello, I have the below regular expression and it usually works, but for some reason when using it in a script it does not want to find filenames if they have a '#' in their first group. Would anyone have a suggestion?

([0-9a-z_#\-]*[0-9a-z_#\-]*)([a-z0-9]{3})([0-9]{3})~([0-9a-z\-]*)\.([a-z]*)$

An example of filename would be:
8c-400-r#lt05000~-.txt

highend
Posts: 13327
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Regular Expression Help

Post by highend »

What's the use of doubling [0-9a-z_#\-]*[0-9a-z_#\-]*?

Apart from that, post the script with which the problem occurs...
One of my scripts helped you out? Please donate via Paypal

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

Re: Regular Expression Help

Post by hermhart »

Please excuse my coding 'skills'. I'm not real good, but I'm trying.

Code: Select all

 end get("selecteditemsnames", "<crlf>") == "", "Please make a selection first.";

 $source = inputselect(<<<EOD
Choose fields to be wildcard(s):
Example fieds:
       1            2       3     4    5
12345678   a05   101   a   txt
EOD
 , <<<EOD
1 Number
2 Document 1
3 Document 2
4 Revision
5 File Extension
EOD
 , <crlf>, 2+8192, , 300, 300, Selection);

       //if (strpos($source, "1") != -1) {
       if (strpos($source, "1 Number") != -1) {
          $result1 = "*";
          //$index++;
          }
       else {
          $result1 = "$1";
          //$index++;
          }

       //if (strpos($source, "2") != -1) {
       if (strpos($source, "2 Document 1") != -1) {
          $result2 = "*";
          //$index++;
          }
       else {
          $result2 = "$2";
          //$index++;
          }

       //if (strpos($source, "3") != -1) {
       if (strpos($source, "3 Document 2") != -1) {
          $result3 = "*";
          //$index++;
          }
       else {
          $result3 = "$3";
          //$index++;
          }

       //if (strpos($source, "4") != -1) {
       if (strpos($source, "4 Revision") != -1) {
          $result4 = "*";
          //$index++;
          }
       else {
          $result4 = "$4";
          //$index++;
          }

       //if (strpos($source, "5") != -1) {
       if (strpos($source, "5 File Extension") != -1) {
          $result5 = "*";
          //$index++;
          }
       else {
          $result5 = "$5";
          //$index++;
          }

 $newResult = $result1.$result2.$result3."~".$result4.".".$result5;

 $selections = get("SelectedItemsNames", "<crlf>");

 Copytext regexreplace($selections, "([0-9a-z_#\-]*[0-9a-z_#\-]*)([a-z0-9]{3})([0-9]{3})~([0-9a-z\-]*)\.([a-z]*)$", $newResult);

    foreach($item, "<clipboard>", "<crlf>", "e") {
    selfilter "$item", , , 1;
   }

highend
Posts: 13327
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Regular Expression Help

Post by highend »

What does this have to do with regular expression(s)?

selfilter isn't working correctly. Why? Look at its
examples in the help file. What does a # stand
for in the pattern? A #? No, it's a metacharacter
for a digit (0-9).

So replace it with [#] (this escapes it!)
before you go into the foreach loop...
One of my scripts helped you out? Please donate via Paypal

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

Re: Regular Expression Help

Post by hermhart »

highend, thank you so much! I have been completely looking/focusing on the wrong thing in the script. :appl:

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

Re: Regular Expression Help

Post by hermhart »

highend,

I finally got my script to work because of what you pointed out to me. But I'm curious, I understand that # can mean to match a number, but I thought the way to escape it would be to do it like this: \#

I'm just curious if you can tell me where in the help that it mentions to use [#] instead of \#.

Just trying to learn for myself.

Thank you.

Post Reply