Search code examples
javaregeximageurl

Java Regex - Ignoring size string in image url


I'm trying to remove the size specifications from image URL strings but I can't seem to find a solution. I don't much about regex so I tried [0-9x] but it only removed all numbers in the url rather than just the dimension substring. I only want to get rid of the parts such as 110x61.

I want to convert my strings from this:

http://techdissected.com/wp-content/uploads/2014/09/google-fiber-rabbit-11-110x61.jpg?6da9e4

http://techdissected.com/wp-content/uploads/2014/09/Nixeus-Headphones-Featured-Image-110x41.jpg?6da9e4

http://techdissected.com/wp-content/uploads/2014/03/Ampedlogo_rac15a_featured-110x94.jpg?6da9e4

to this:

http://techdissected.com/wp-content/uploads/2014/09/google-fiber-rabbit-11.jpg?6da9e4

http://techdissected.com/wp-content/uploads/2014/09/Nixeus-Headphones-Featured-Image.jpg?6da9e4

http://techdissected.com/wp-content/uploads/2014/03/Ampedlogo_rac15a_featured.jpg?6da9e4

I'm using RegexPlanet for testing the patterns but none of what I've come up with works... What regular expression would solve my issue? Any help would be appreciated. Extra points for removing the trailing ?6da9e4

I found an interesting solution here but it doesn't seem to work in Java.


Solution

  • The regex -\d{1,4}x\d{1,4}

    which breaks down to:

    - : the literal '-', followed by
    \d{1,4}: any numeric character, one to four times, followed by
    x : the literal 'x', followed by
    \d{1,4}: any numeric character, one to four times
    

    will work for you in Java

    String input = "http://techdissected.com/wp-content/uploads/2014/09/Nixeus-Headphones-Featured-Image-110x41.jpg?6da9e4";  
    input = input.replaceAll("-\\d{1,4}x\\d{1,4}", "");
    System.out.println(input); 
    //prints: http://techdissected.com/wp-content/uploads/2014/09/Nixeus-Headphones-Featured-Image.jpg?6da9e4