Problem with Regular Expression

2

I have this regular expression in php that should find the default in the name of a file. Example: dge_ANEXO_II_F_TJ[1].pdf But I can not get it to work.

Can anyone help me?

'/[a-z]{3}_ANEXO|Anexo|anexo_[A-Za-z]{1,7}_[A-Z]{1}_[A-Z0-9]{2,5}....pdf/'
    
asked by anonymous 10.11.2015 / 20:12

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/

Try RegEx101 .


More complete solution, providing brackets:

The user @DiegoFelipe has posted to the comments an improved solution, which switches the "wildcards" "( ... ) by the brackets ( \[[0-9]{1}\] ) (note 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 .

Issue @Guilherme Lautert

If the word "attachment" is not case-sensitive, lowercase:

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

Test RegEx101.

A slightly shorter alternative if it is rigorous:

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

Test RegEx101.

    
13.04.2017 / 14:59