开发者

Validate File names

开发者 https://www.devze.com 2023-03-27 21:30 出处:网络
File Format # [filename].[2 letter locale].[outputformat - html/subject/text].xml Valid filenames - myname.en.html.xml

File Format # [filename].[2 letter locale].[outputformat - html/subject/text].xml

Valid filenames -

myname.en.html.xml

myname2.pt.subject.xml etc.

Also, filenames are coming from a column in the database.

Can somebody help me with the regex?

Thank You!

Edit# as per @drf

public static boolean isValidFileName( String fileName ) {
    String expression = "^\\w+\\.[A-Z]{2}\\.(?>html|subject|text)\\.xml$" ; 
    CharSequen开发者_JS百科ce inputStr = expression ;
    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    return matcher.matches();
}

public static void main(String[] args) {
    String fileName = "somefile.en.html.xml" ;
    System.out.println("Is valid file name # " + fileName + " - " + isValidSEASFileName(fileName)  ) ;  
}


If a conservative file name validation is acceptable (letters and numbers only, no maximum length) and the two-letter locale can be any two letters, then something like this (set to case-insensitive) could work:

 ^\w+\.[A-Z]{2}\.(?>html|subject|text)\.xml$


Try the following:

[^\.]*\.[a-z]{2}\.(html|subject|text)\.xml
0

精彩评论

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

关注公众号