Problem with Regular Expression

Asked

Viewed 79 times

2

I have this regular expression in php that should find the pattern in a file name. Example: dge_ANEXO_II_F_TJ[1].pdf But I can’t make it work.

Can someone help me?

'/[a-z]{3}_ANEXO|Anexo|anexo_[A-Za-z]{1,7}_[A-Z]{1}_[A-Z0-9]{2,5}....pdf/'

1 answer

6

Just add parentheses in the search for anexo:

/[a-z]{3}_(ANEXO|Anexo|anexo)_[A-Za-z]{1,7}_[A-Z]{1}_[A-Z0-9]{2,5}....pdf/

Test in Regex101.


More complete solution, providing the brackets:

The user @Diegofelipe posted us comments an improved solution, which changes the "wild cards" (...) by the square brackets (\[[0-9]{1}\])(notice the difference at the end):

/[a-z]{3}_(ANEXO|Anexo|anexo)_[A-Za-z]{1,7}_[A-Z]{1}_[A-Z0-9]{2,5}\[[0-9]{1}\].‌​pdf$/

See the test on IDEONE.

Edition @Guilherme Lautert

If the word "attachment" is case-insensitive:

[a-z]{3}_((?i)anexo)_[A-Za-z]{1,7}_[A-Z]{1}_[A-Z0-9]{2,5}...\.pdf 

Test in Regex101.

A little more "short" alternative if you have rigor:

[a-z]{3}_(ANEXO|(A|a)nexo)_[A-Za-z]{1,7}_[A-Z]{1}_[A-Z0-9]{2,5}...\.pdf

Test in Regex101.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.