简体   繁体   中英

Regex for match web extensions

I want to check whether a url is a image, java script, pdf etc

String url = "www.something.com/script/sample.js?xyz=xyz;

Below regex works fine but only with out xyz=zyz

".*(mov|jpg|gif|pdf|js)$"

When i remove $ at the end to eliminate regex requirement for .js to be in end but then it gives false

.*(mov|jpg|gif|pdf|js).*$ allows you to have any optional text after the file extension. The capturing group captures the file extension. You can see this here .

Use the regex as below:

   .*\\.(mov|jpg|gif|pdf|js)\\?

This matches for dot(.) followed by your extension and terminated by ?

The first dot(.) is matching any character while second dot(.) prefixed by \\\\ match for dot(.) as literal just before your extension list.

Why not use java.net.URL to parse the url string, it could avoid lots of mismatching problems:

try {
  URL url = new URL(urlString);
  String filename = url.getFile();
  // now test if the filename ends with your desired extensions.
} catch (Exception e) {
  // This case the url cannot be parsed.
}

I'm not a big fan of this, but try:

.*\\.(mov|jpg|gif|pdf|js).*$

The problem is that it will accept things like "our.moving.day"

and post your code. there is always more than one way to skin a cat and perhaps there is something wrong with your code, not the regex.

Also, try regex testers...theres a ton of them out there. i'm a big fan of:

http://rubular.com/ and http://gskinner.com/RegExr/ (but they are mostly for php/ruby)

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