Set regex pattern based on filename

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

Set regex pattern based on filename

Post by hermhart »

I've been struggling with the following and wondering if anyone could help me out with how to set a regular expression pattern based on a filename.

I have a list of files that all follow the same pattern of either of the following three files, what I need to do is, based on the pattern of each of the three filenames, is set which regex expression pattern to use for the rest of the loop I have.

1234567_-.txt
1234567_-_a_1234567---new000.txt
1234567_-_m_1234567--.txt

I know the following code does not work, but I cannot figure out what I need to with this. If anyone has a better solution or knows how to tweak this, I would greatly appreciate it.

Code: Select all

 $matches = regexmatches("$file", "(.*)_([0-9a-zA-Z\-]*)_(s|m|a)_(.*)-([0-9a-zA-Z\-]*)-new([0-9]*)(\.)(txt)$", <crlf>);
 $count = gettoken($matches, "count", <crlf>);
 if (gettoken($matches, "count", <crlf>) > 5){
        $fRegExPat = "(.*)_([0-9a-zA-Z\-]*)_(s|m|a)_(.*)-([0-9a-zA-Z\-]*)-new([0-9]*)(\.)(txt)$";
    }
    elseif (gettoken($matches, "count", <crlf>) > 3) {
        $fRegExPat = "(.*)_([0-9a-zA-Z\-]*)_(s|m|a)_(.*)(\.)(txt)$";
    }
    else {
        $fRegExPat = "(.*)_([0-9a-zA-Z\-]*)(\.)(txt)$";
    }
Windows 11 Pro; 100% scaling

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

Re: Set regex pattern based on filename

Post by highend »

This matches all three but if it works for other file name: No clue, you aren't providing all possible names...

(.*?)_([0-9a-z-]*?-)(_(s|m|a)_(.*?)-([0-9a-z-]*?)-new([0-9]*?))?(\.txt)$
One of my scripts helped you out? Please donate via Paypal

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

Re: Set regex pattern based on filename

Post by hermhart »

Thank you for the feedback, but I may have stated the question wrong. The list of all the files that I have in a directory will match either one of the three filename patterns that I have below:
(.*)_([0-9a-zA-Z\-]{1,2})(\.)(txt)
(.*)_([0-9a-zA-Z\-]{1,2})_(s|m|a)_(.*)-([0-9a-zA-Z\-]{1,2})-new([0-9]*)(\.)(txt)
(.*)_([0-9a-zA-Z\-]{1,2})_(s|m|a)_(.*)-([0-9a-zA-Z\-]{1,2})(\.)(txt)

Examples of these files would be:
1234567_-.txt
1234567_-_a_1234567---new000.txt
1234567_-_m_1234567--.txt

I'm trying to figure out that:
if I come across a file like: 1234567_-.txt
which matches this pattern: (.*)_([0-9a-zA-Z\-]{1,2})(\.)(txt)
then use that regex

elseif I come across a file like: 1234567_-_a_1234567---new000.txt
which matches this pattern: (.*)_([0-9a-zA-Z\-]{1,2})_(s|m|a)_(.*)-([0-9a-zA-Z\-]{1,2})-new([0-9]*)(\.)(txt)
then use that regex

elseif I come across a file like: 1234567_-_m_1234567--.txt
which matches this pattern: (.*)_([0-9a-zA-Z\-]{1,2})_(s|m|a)_(.*)-([0-9a-zA-Z\-]{1,2})(\.)(txt)
then use that regex

Sorry for any confusion and I hope this makes a little more sense.
Windows 11 Pro; 100% scaling

jupe
Posts: 3570
Joined: 20 Oct 2017 21:14
Location: Win10 22H2 120dpi

Re: Set regex pattern based on filename

Post by jupe »

I think what highend meant was you can use 1 regex pattern that matches all 3 types of files, instead of 3 regex patterns, anyway if you are adverse to that, then below is an alternative method.

Code: Select all

  $file = "1234567_-_a_1234567---new000.txt";
  foreach($fRegExPatS, "(.*)_([0-9a-zA-Z\-]*)_(s|m|a)_(.*)-([0-9a-zA-Z\-]*)-new([0-9]*)(\.)(txt)$~(.*)_([0-9a-zA-Z\-]*)_(s|m|a)_(.*)(\.)(txt)$~(.*)_([0-9a-zA-Z\-]*)(\.)(txt)$", "~") {
    if (regexmatches($file, $fRegExPatS)) { $fRegExPat = $fRegExPatS; break; }
  }
  e $fRegExPat;

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

Re: Set regex pattern based on filename

Post by highend »

I don't even know why you want a pattern in the end instead of the files that match one...
I think what highend meant was you can use 1 regex pattern that matches all 3 types of files
Not exactly. I was only showing one pattern hoping that he is able to break it down into three by himself again :ninja:

Apart from that, general rules of thumb:
- Make your patterns lazy instead of greedy
- a-zA-Z is just unnecessary when you don't use the flag for case-sensitivity in the script command
- You don't need to escape the - in a range if it's not followed by (the second half of) a range, e.g. [0-9a-z-]
- Don't create groups for things that don't need to be grouped, e.g. your (\.)(txt)
One of my scripts helped you out? Please donate via Paypal

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

Re: Set regex pattern based on filename

Post by hermhart »

Thank you both for the help. jupe, it looks like what you did is what I need! I would not have figured that out on my own. And highend, thank you for the information on the regular expression. It takes me a while to figure those out, as I am certainly not a pro with them.
Windows 11 Pro; 100% scaling

Post Reply