0

Good morning everyone,

Here is my issue: I have the file name test1.pdf and I want to check if the last character before .pdf is a digit. If it is, I want to extract it. The following regex work:

(\D*)(\d?)(\.pdf)

I get my 3 elements: test/1/.pdf

But now my file is named test1test1.pdf. So I try this regex;

(\w*)(\d?)(\.pdf)

My regex isn't working anymore because I get: test1test1/.pdf instead of test1test/1/.pdf. So the last digit before the .pdf is included in (\w*)

This regex will solve this issue:

(\w*)(\d{1})(\.pdf)

But it won't work if there is no digit before the .pdf. It won't match test1test.pdf for example.

I know some regex that will fix my problem but I find them not really elegant. Is there a way to force a digit to match \d rather than \w or dot, or is there a way to specify "end of parenthesis" like "end of string" with $?

Thank's for your help or tips (btw I'm using PHP and the preg_match() function)

0 Answers0