2

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.

0

3 Answers 3

2

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)")

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

Sign up to request clarification or add additional context in comments.

1 Comment

thanks it worked, and i have change it to single regex i didn't realized that it will have to compile 4 different regexes.
1

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");

Comments

1

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");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.