简体   繁体   中英

Regular Expressions code in java

I want to skip the url which contains any office file format or pdf at the end of url here is my code.

String Url ="http://chemistry.csudh.edu/faculty/jim/aromaticity.ppt";

        if (!Url.matches(".*(doc|dot|docx|docm|dotx|dotm)")
                || !Url.matches(".*ppt|pot|pps")
                || !Url.matches(".*xls|xlt|xlm")
                || !Url.matches(".*pdf"))
            System.out.print(Url);
        else
            System.out.print("true");

I want to know what is wrong with this code fragment as it prints url every time but i want to skip the url which contain any of above format.

You are missing the parenthesis in the second and third regex. !Url.matches(".*ppt|pot|pps") will match all the URLs that doesn't end with ppt , but the URL like abc.pot will not be matched by that regex, and the condition will be true . you should change it to:

!Url.matches(".*(ppt|pot|pps)")

.. as in the first regex. Also, that should be && instead of || in your condition.

BTW, why do you have 4 different matches() invocation? That will have to compile 4 different regexes, while you could have done it with a single regex. just add all the extensions to the first regex list:

if (!url.matches(".*(doc|dot|docx|docm|dotx|dotm|ppt|pot|pps|xls|xlt|xlm|pdf)")

PS: Kindly follow Java Naming Conventions. Variable names should start with lower-case alphabets.

It's a logic error, you should change it to

String Url ="http://chemistry.csudh.edu/faculty/jim/aromaticity.ppt";

    if (!Url.matches(".*(doc|dot|docx|docm|dotx|dotm)")
            && !Url.matches(".*ppt|pot|pps")
            && !Url.matches(".*xls|xlt|xlm")
            && !Url.matches(".*pdf"))
        System.out.print(Url);
    else
        System.out.print("true");

Your condition if faulty, since you're using || instead of && . Consider the following - a URL that ends in .pdf cannot end in .doc , and vice versa - so the condition will always evaluate to true . Logically, you want to test that a URL does not match any document format - with infers using && :

String Url ="http://chemistry.csudh.edu/faculty/jim/aromaticity.ppt";

if (!Url.matches(".*(doc|dot|docx|docm|dotx|dotm)")
        && !Url.matches(".*ppt|pot|pps")
        && !Url.matches(".*xls|xlt|xlm")
        && !Url.matches(".*pdf"))
    System.out.print(Url);
else {
    System.out.print("true");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM