Java 如何用单斜杠替换网址的双斜杠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22173575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to replace double slash with single slash for an url
提问by
For the given url like "http://google.com//view/All/builds", i want to replace the double slash with single slash. For example the above url should display as "http://google.com/view/All/builds"
对于像“ http://google.com//view/All/builds”这样的给定网址,我想用单斜线替换双斜线。例如,上面的网址应显示为“ http://google.com/view/All/builds”
I dint know regular expressions. Can any one help me, how can i achieve this using regular expressions.
我不知道正则表达式。任何人都可以帮助我,我如何使用正则表达式实现这一目标。
采纳答案by Laurent B
To avoid replacing the first // in http://
use the following regex :
为了避免替换第一个 //http://
使用以下正则表达式:
String to = from.replaceAll("(?<!http:)//", "/");
PS: if you want to handle https use (?<!(http:|https:))//
instead.
PS:如果您想处理 https,请(?<!(http:|https:))//
改用。
回答by poitevinpm
I suggest you simply use String.replace which documentation is http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace(java.lang.CharSequence, java.lang.CharSequence)
我建议你简单地使用 String.replace 文档是http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace(java.lang.CharSequence, java.lang.CharSequence )
Something like `myString.replace("//", "/");
类似`myString.replace("//", "/");
If you want to remove the first occurence:
如果要删除第一次出现:
String[] parts = str.split("//", 2);
str = parts[0] + "//" + parts[1].replaceAll("//", "/");
String[] parts = str.split("//", 2);
str = parts[0] + "//" + parts[1].replaceAll("//", "/");
Which is the simplest way (without regular expression). I don't know the regular expression corresponding, if there is an expert looking at the thread.... ;)
这是最简单的方法(没有正则表达式)。我不知道对应的正则表达式,如果有高手在看线程....;)
回答by Qinjin
String to = from.replaceAll("(?<!(http:|https:))[//]+", "/");
will match two or more slashes.
将匹配两个或多个斜线。
回答by am0wa
Here is the regexp:
这是正则表达式:
/(?<=[^:\s])(\/+\/)/g
/(?<=[^:\s])(\/+\/)/g
It finds multiple slashes in url preserving ones after protocol regardless of it.
Handles also protocol relative urls which start from //
.
无论它如何,它都会在协议之后在保留 url 的 url 中找到多个斜杠。
还处理从//
.
@Test
public void shouldReplaceMultipleSlashes() {
assertEquals("http://google.com/?q=hi", replaceMultipleSlashes("http://google.com///?q=hi"));
assertEquals("https://google.com/?q=hi", replaceMultipleSlashes("https:////google.com//?q=hi"));
assertEquals("//somecdn.com/foo/", replaceMultipleSlashes("//somecdn.com/foo///"));
}
private static String replaceMultipleSlashes(String url) {
return url.replaceAll("(?<=[^:\s])(\/+\/)", "/");
}
Literally means:
字面意思是:
(\/+\/)
- find group:/+
one or more slashes followed by/
slash(?<=[^:\s])
- which follows the group (*posiive lookbehind)of this (*negated set)[^:\s]
that excludes:
colon and\s
whitespaceg
- global search flag
(\/+\/)
- 查找组:/+
一个或多个斜杠后跟/
斜杠(?<=[^:\s])
- 跟随这个(*否定集)的组(*正后视)[^:\s]
,不包括:
冒号和\s
空格g
- 全局搜索标志
回答by Christian Hujer
Is Regex the right approach?
正则表达式是正确的方法吗?
In case you wanted this solution as part of an exercise to improve your regex skills, then fine. But what is it that you're really trying to achieve? You're probably trying to normalize a URL. Replacing //
with /
is one aspect of normalizing a URL. But what about other aspects, like removing redundant ./
and collapsing ../
with their parent directories? What about different protocols? What about ///
? What about the //
at the start? What about ///
at the start in case of file:///
?
如果您希望将此解决方案作为练习的一部分来提高您的正则表达式技能,那么很好。但你真正想要达到的是什么?您可能正在尝试规范化 URL。更换//
同/
是一个标准化URL的一个方面。但是其他方面呢,比如删除冗余./
和折叠../
它们的父目录?不同的协议呢?怎么样///
?怎么样//
在开始?怎么样///
在的情况下开始file:///
?
If you want to write a generic, reusable piece of code, using a regular expression is probably not the best appraoch. And it's reinventing the wheel. Instead, consider java.net.URI.normalize()
.
如果你想写一段通用的、可重用的代码,使用正则表达式可能不是最好的方法。它正在重新发明轮子。相反,请考虑java.net.URI.normalize()
.
java.net.URI.normalize()
java.net.URI.normalize()
java.lang.String
java.lang.String
String inputUrl = "http://localhost:1234//foo//bar//buzz";
String normalizedUrl = new URI(inputUrl).normalize().toString();
java.net.URL
java.net.URL
URL inputUrl = new URL("http://localhost:1234//foo//bar//buzz");
URL normalizedUrl = inputUrl.toURI().normalize().toURL();
java.net.URI
java.net.URI
URI inputUri = new URI("http://localhost:1234//foo//bar//buzz");
URI normalizedUri = inputUri.normalize();
Regex
正则表达式
In case you dowant to use a regular expression, think of all possibilities. What if, in future, this should also process other protocols, like https
, file
, ftp
, fish
, and so on? So, think again, and probably use URI.normalize()
. But if you insist on a regular expression, maybe use this one:
如果您确实想使用正则表达式,请考虑所有可能性。如果将来这也应该处理其他协议,例如https
、file
、ftp
、 等fish
,该怎么办?所以,再想一想,可能会使用URI.normalize()
. 但是如果你坚持使用正则表达式,也许可以使用这个:
String noramlizedUri = uri.replaceAll("(?<!\w+:/?)//+", "/");
Compared to other solutions, this works with all URLs that look similar to HTTP URLs just with different protocols instead of http
, like https
, file
, ftp
and so on, and it will keep the triple-slash ///
in case of file:///
. But, unlike java.net.URI.normalize()
, this does not remove redundant ./
, it does not collapse ../
with their parent directories, it does not other aspects of URL normalization that you and I might have forgotten about, and it will not be updated automatically with newer RFCs about URLs, URIs, and such.
其它解决方案相比,该作品与所有网址类似于HTTP网址,只是不同的协议,而不是http
像https
,file
,ftp
等等,它会保持三斜杠///
的情况下file:///
。但是,与 不同的是java.net.URI.normalize()
,这不会删除多余的./
,它不会../
与它们的父目录一起折叠,它不会与您和我可能忘记的 URL 规范化的其他方面一起使用,并且不会使用有关 URL、URI 的较新 RFC 自动更新,诸如此类。