Page 1 of 1

Set regex pattern based on filename

Posted: 14 Aug 2021 20:06
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)$";
    }

Re: Set regex pattern based on filename

Posted: 14 Aug 2021 22:40
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)$

Re: Set regex pattern based on filename

Posted: 15 Aug 2021 00:16
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.

Re: Set regex pattern based on filename

Posted: 15 Aug 2021 01:39
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;

Re: Set regex pattern based on filename

Posted: 15 Aug 2021 12:15
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)

Re: Set regex pattern based on filename

Posted: 16 Aug 2021 15:44
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.