1
votes

I have a PHP file that redirects the user to the right user manual, depending on the link they clicked on. Since we have a lot of manuals (some are former versions, we need to match with the latest), I'm forced to use a REGEX to find what I want.

Let's use fruits for example. In store, we have manual names that can look like that :

$manuals = [
    'dc-cn-13apple-frv4-5.pdf',
    'dc-cn-33_banana-strawberryv10-108.pdf',
    'dc-cn-32peachenv10-125.pdf',
    'dc-cn-23lemoncoconutwatermelonitv09-130.pdf'
];

So there's a lot of possible combination, what's important is that they always start with 'dc-cn-', then a 2 digits number that's important for us (it's like the reference of the fruit).

It continues with some words to describe the fruit followed immediately by 2 letters to know in which language the manual is, and a 'v08' or 'v10' to know the version. Sometimes those 2 letters and numbers for the version come with a dash (like '-frv4').

Finally, they always end with a random 3 digits number (automatically given by the CMS we use), followed by '.pdf'.

I tried to manipulate my array like this:

foreach ($manuals as $manual) {
    if(preg_match('/^[0-9][0-9][\.]pdf$/', $manual)) {
        echo 'OK';
    }
}

But so far my REGEX doesn't match anything and I really feel I have no clue of what I'm doing.

It would really help me to be able to inspect the first number ('dc-cn-32', 'dc-cn-13'), the 2 letters at the end of the word ('-frv4', 'peachenv10' or 'melonitv09-130.pdf'), and the 3 last digits ('melonitv09-130.pdf').

Then I would preg_split it all and start to compare.

1
Why is there no language code for the banana strawberry line? Why is there only a single digit number in the first line? - Sweeper
please explain in a more simple way. - nAviD
The fact that the language code is optional makes it rather hard to come up with a generic solution. See this demo, ry is identified as the lang code. - Wiktor Stribiżew
Sweeper, I have no idea myself. The company had a lot of different workers those last year, and each and every one of them had it's own way of naming the manuals. I'm currently trying to persuade them to let me change those names to a standarded format. In a more simple way, I need to preg_split those strings in order to compare some of their numbers. - Hadrien Patricola

1 Answers

1
votes

Your examples don't necessarily match your description, but per description, this regex should work (not tested though):

^dc-cn-\d{2}[a-zA-Z_\-]+-?\w{2}v\d+-\d{3}\.pdf$

Explanation:

  • ^dc-cn- matches your prefix from the beginning of the string
  • followed by \d{2} - a two-digit number
  • followed by [a-zA-Z_\-] - a string of 1 or more letters, hyphens or underscores (from your examples)
  • followed by -? - a possible dash that may or may not be there
  • followed by \w{2} - a two-letter language code
  • followed by v\d+ - letter v and a number
  • followed by -\d{3} - a 3-digit CMS number
  • followed by \.pdf$ - literally, .pdf at the end of the string

UPDATE: If the last CMS number is not always a 3-digit one, but can be 1 or 2 digits (e.g. as in one of the examples above, then the regex becomes

^dc-cn-\d{2}[a-zA-Z_\-]+-?\w{2}v\d+-\d{1,3}\.pdf$