kokoko
kokoko

Reputation: 25

How can i write a more complex regexp in java?

id like to do the following rules:

Not begining with space " ", not more than one "." , not begining with "." not end with "." not ? / \ : ; in the file.

public static void Invalid(String[] filename){  
for(String s: filename){     
    String u  = s;
    try {
        u = new String(s.getBytes(), "UTF-8");
        System.out.println(u);
    } catch (Exception e) {
        e.printStackTrace();
    } 
    u=u.replaceAll("[.$]", " ");//.{2,}\\?
    u = u.replaceAll("\\s+", "_");
    System.out.println(s + " = " + u);
}
}

in main just simple:

  String[] name={"some?thing..txt."};
  Invalid(name);

output:

 some?thing..txt.
 some?thing..txt. = some_thing_txt_

how can i do that?

Upvotes: 1

Views: 71

Answers (1)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

You can use this pattern to validate the filename:

^[^?/\\\\;: .][^?/\\\\;:.]*(?:\\.[^?/\\\\;:.]+)?$

Note: if you want to allow the empty string, add |^$ at the end.

Not sure what you exactly want to do with invalid filenames.

Upvotes: 1

Related Questions