OVH Cloud OVH Cloud

demande d'avis sur une regexpr

5 réponses
Avatar
Guillaume D.
Pour tester si un File a l'extension "xml", je fais ça :

return Pattern.compile("^.*\\.xml$").matcher(file.getName()).matches();


Néammoins, ça me semble une ecriture assez lourde, et en plus ca m'a
l'air case sensitive.

Une proposition pour simplifier et optimiser le bazar ?

Guillaume D.

5 réponses

Avatar
Jean-Christophe Garnier
Bonjour,

return Pattern.compile(".*.xml").matcher(file.getName()).matches();

fait l'affaire. Qui dit mieux ?
JC


Pour tester si un File a l'extension "xml", je fais ça :

return Pattern.compile("^.*.xml$").matcher(file.getName()).matches();


Néammoins, ça me semble une ecriture assez lourde, et en plus ca m'a
l'air case sensitive.

Une proposition pour simplifier et optimiser le bazar ?

Guillaume D.


Avatar
Guillaume D.
Jean-Christophe Garnier wrote:
Bonjour,

return Pattern.compile(".*.xml").matcher(file.getName()).matches();


Si tu supprimes la marque de fin de chaine ($), un fichier de nom
"toto.xmlfoo" passera pour valide.

Voilà ma version actuelle (avec case insensitive) :

return Pattern.compile("^.*.xml$",
Pattern.CASE_INSENSITIVE).matcher(file.getName()).matches();

Guillaume D.

Avatar
Eric Frigot
Jean-Christophe Garnier wrote:
Bonjour,

return Pattern.compile(".*.xml").matcher(file.getName()).matches();

fait l'affaire. Qui dit mieux ?
JC


Pour tester si un File a l'extension "xml", je fais ça :

return Pattern.compile("^.*.xml$").matcher(file.getName()).matches();


Néammoins, ça me semble une ecriture assez lourde, et en plus ca m'a
l'air case sensitive.

Une proposition pour simplifier et optimiser le bazar ?

Guillaume D.





Si le but est uniquement de tester si un fichier porte l'extension
"xml", il suffit de

return file.getName().toLowerCase().endsWith("xml");

pas besoin de regexp.

Eric.


Avatar
Guillaume D.
Eric Frigot wrote:
Jean-Christophe Garnier wrote:

Bonjour,

return Pattern.compile(".*.xml").matcher(file.getName()).matches();

fait l'affaire. Qui dit mieux ?
JC


Pour tester si un File a l'extension "xml", je fais ça :

return Pattern.compile("^.*.xml$").matcher(file.getName()).matches();


Néammoins, ça me semble une ecriture assez lourde, et en plus ca m'a
l'air case sensitive.

Une proposition pour simplifier et optimiser le bazar ?

Guillaume D.






Si le but est uniquement de tester si un fichier porte l'extension
"xml", il suffit de

return file.getName().toLowerCase().endsWith("xml");

pas besoin de regexp.


Là ca tolère des fichiers du genre ".totoxml" mais soit, on peut adapter
avec :

return file.getName().toLowerCase().endsWith(".xml");

Merci Eric.

Guillaume D.



Avatar
Vincent Lascaux
return Pattern.compile(".*.xml").matcher(file.getName()).matches();


Si tu supprimes la marque de fin de chaine ($), un fichier de nom
"toto.xmlfoo" passera pour valide.


Tiré de http://java.sun.com/j2se/1.4.2/docs/api/

matches
public boolean matches()Attempts to match the entire input sequence against
the pattern.
If the match succeeds then more information can be obtained via the start,
end, and group methods.



Returns:
true if, and only if, the entire input sequence matches this matcher's
pattern
Entire input, ca veut dire qu'il faut que toute la chaine matche
l'expression non ?

--
Vincent