Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
Is the hyphen always before the substring? i.e -110x61 ? –  hwnd Sep 26 '14 at 1:20
    
see the demo –  Baby Sep 26 '14 at 1:26
    
@hwnd Yes, there is always a hyphen before the substring. –  Pkmmte Sep 26 '14 at 23:44
    
@TheQuickBrownFox Thanks, that seems to work great and that website is very helpful! –  Pkmmte Sep 26 '14 at 23:44

5 Answers 5

up vote 2 down vote accepted

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
share|improve this answer
    
Thank you, this worked perfectly for me! Is there a way to remove the trailing "?6da9e4" as part of this regex? Someone suggested I use "|\\?.*" at the end of the regex to fix this but it didn't work. –  Pkmmte Sep 27 '14 at 1:34

If the hyphen (-) is always constant before the dimensions, you could use the following.

url = url.replaceAll("-\\d+x\\d+", "");
// http://techdissected.com/wp-content/uploads/2014/09/google-fiber-rabbit-11.jpg?6da9e4

To remove both the dimension and the trailing query:

url = url.replaceAll("-\\d+x\\d+|\\?.*", "");
// http://techdissected.com/wp-content/uploads/2014/09/google-fiber-rabbit-11.jpg
share|improve this answer

This regex works:

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

String newUrl = url.replaceAll("-[0-9]+x[0-9]+", "");

System.out.println(newUrl);

Output:

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

If you want to keep the hyphen here -110x61 use url.replaceAll("[0-9]+x[0-9]+", "");

share|improve this answer

Not sure about java, but this might work for any dimensions:

/(-\d+x\d+)/g
share|improve this answer

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

String replaceAll = url.replaceAll("-\\d*x\\d*", ""); System.out.println(replaceAll);

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.