开发者

Java Reg-ex to validate file convention

开发者 https://www.devze.com 2023-03-31 00:04 出处:网络
How to check if the following is part of the file name using regular expressions .en. .fr. .pt. .de. .ja. .es开发者_运维技巧.

How to check if the following is part of the file name using regular expressions

.en.
.fr.
.pt.
.de.
.ja.
.es开发者_运维技巧.
.it.
.cn.

I know one other way is to check the index of and return boolean.


Just do a match of \.(en|fr|pt|de|ja|es|it|cn|)\.


Just use the regex...

.*\.(?:en|fr|pt|de|ja|es|it|cn)\..*


Try this expression: \.(?:en|fr|pt|de|ja|es|it|cn)\. or for String#matches(): .*\.(?:en|fr|pt|de|ja|es|it|cn)\..* (note that in string literals, you need to escape the \\: ".*\\.(?:en|fr|pt|de|ja|es|it|cn)\\..*")


string.matches(".*\\.(en|fr|pt|de|ja|es|it|cn)\\..*");

or you can precompile the pattern with

Pattern p = Pattern.compile(".*\\.(en|fr|pt|de|ja|es|it|cn)\\..*");

and check with p.matches(string);

0

精彩评论

暂无评论...
验证码 换一张
取 消