2014-09-26 16 views
0

我想从图像URL字符串中删除大小规范,但我似乎无法找到解决方案。我对正则表达式没有太多的了解,所以我尝试了[0-9x],但它只删除了url中的所有数字,而不仅仅是维度子字符串。我只想摆脱如110x61这样的零件。Java的正则表达式 - 忽略图像url中的大小字符串

我想我的琴弦从这个转换:

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 

这样:

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 

我使用RegexPlanet测试模式,但没有什么,我想出来的作品... 什么正则表达式可以解决我的问题?任何帮助,将不胜感激。删除尾部的加分?6da9e4

我找到了一个有趣的解决方案here,但它似乎在Java中不起作用。

+0

连字符总是在子串之前吗?即“-110x61”? – hwnd 2014-09-26 01:20:59

+0

请参阅[demo](http://www.regexr.com/39iqt) – Baby 2014-09-26 01:26:42

+0

@hwnd是的,子字符串前总是有一个连字符。 – Pkmmte 2014-09-26 23:44:10

回答

2

正则表达式-\d{1,4}x\d{1,4}

其分解为:

- : 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 

会为你在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 
+0

谢谢,这对我来说非常合适! 有没有办法删除尾部“?6da9e4”作为这个正则表达式的一部分?有人建议我在正则表达式的末尾使用“| \\?。*”来解决这个问题,但它不起作用。 – Pkmmte 2014-09-27 01:34:56

0

不知道关于Java,但是这可能对于任何维度的工作原理:

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

这个正则表达式的工作原理:

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

输出:

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

如果你想保持这里的连字符-110x61使用url.replaceAll("[0-9]+x[0-9]+", "");

1

如果连字符(-)是尺寸前总是不变的,你可以使用以下内容。

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

删除这两个维度和后行查询:

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