Page 1 of 1

If regexmatches true

Posted: 12 Aug 2021 18:46
by hermhart
I am trying to do an IF statement to see if a regexmatches, and if it does to set a pattern. I'm having an issue trying to:
  • Understand why, without quotes around the $file = 1234567_-.txt it seems to drop the period before "txt" in the value for $file.
  • And not sure why it seems that the first line of the IF statement doesn't match, do I have the regex pattern wrong?

Code: Select all

 $file = 1234567_-.txt;

 if (regexmatches($file, "(.*)_([0-9a-zA-Z\-]*)(\.)(txt)$") == "1") {
        $fRegExPat = "(.*)_([0-9a-zA-Z\-]*)(\.)(txt)$";
    }
    else {
        $fRegExPat = "No match";
    }

 echo $fRegExPat;

Re: If regexmatches true

Posted: 12 Aug 2021 21:39
by jupe
So I would assume since you are trying to use regexmatches() you would have read the help for that command, where it is documented that the return from regexmatches is the list of matches not 1 or 0, also all strings need quoting so without the quotes the period is used to join the strings instead.

Re: If regexmatches true

Posted: 12 Aug 2021 21:48
by hermhart
Thank you. I will look at that a bit deeper. I think I was getting too frustrated when I couldn't get things to work.