Reputation: 802
I'm trying to match just the file extension on a URL to an image.. I've got this regex:
(?<=\.)(jpe?g|png|gif|bmp|tga|tiff)$
I've tested this on several websites and it matches just fine.. but when I try to use it in Java I added an extra \ to escape the . and I don't think I should have to add more than that? but this isn't working:
Pattern extensionPat = Pattern.compile("(?<=\\.)(jpe?g|png|gif|bmp|tga|tiff)$", Pattern.CASE_INSENSITIVE);
Matcher findExtension = extensionPat.matcher(imageURL);
String extension = findExtension.group();
where imageURL is "https://www.google.com/images/srpr/logo4w.png"
At a coworker's recommendation I tried escaping the pipes as well into:
Pattern extensionPat = Pattern.compile("(?<=\\.)(jpe?g\\|png\\|gif\\|bmp\\|tga\\|tiff)$", Pattern.CASE_INSENSITIVE);
I've tried escaping the $, and the <, and nothing seems to work...
Upvotes: 0
Views: 81
Reputation: 46239
You need to call findExtension.find()
first, otherwise findExtension.group()
returns nothing.
Upvotes: 3