0

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.

4
  • Is the hyphen always before the substring? i.e -110x61 ? Commented Sep 26, 2014 at 1:20
  • see the demo Commented Sep 26, 2014 at 1:26
  • @hwnd Yes, there is always a hyphen before the substring. Commented Sep 26, 2014 at 23:44
  • @TheQuickBrownFox Thanks, that seems to work great and that website is very helpful! Commented Sep 26, 2014 at 23:44

5 Answers 5

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

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

Comments

1

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]+", "");

Comments

0

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

/(-\d+x\d+)/g

Comments

0

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

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.