Java点与“任何字符”不匹配
发布时间:2020-12-15 02:04:20 所属栏目:Java 来源:网络整理
导读:点应匹配任何字符.那么为什么这个正则表达式不起作用呢? String url = "http://wikipedia.org";System.out.println(url.replace("htt.://","")); 输出:http://wikipedia.org 解决方法 String.replace() 编译文字正则表达式.你应该使用 String.replaceAll(
|
点应匹配任何字符.那么为什么这个正则表达式不起作用呢?
String url = "http://wikipedia.org";
System.out.println(url.replace("htt.://",""));
输出:http://wikipedia.org 解决方法String.replace()编译文字正则表达式.你应该使用
String.replaceAll()或
String.replaceFirst():
String url = "http://wikipedia.org";
System.out.println(url.replaceFirst("htt.://",""));
这是来自Java源代码的方法.replace(): /**
* Replaces each substring of this string that matches the literal target
* sequence with the specified literal replacement sequence. The
* replacement proceeds from the beginning of the string to the end,for
* example,replacing "aa" with "b" in the string "aaa" will result in
* "ba" rather than "ab".
*
* @param target The sequence of char values to be replaced
* @param replacement The replacement sequence of char values
* @return The resulting string
* @throws NullPointerException if <code>target</code> or
* <code>replacement</code> is <code>null</code>.
* @since 1.5
*/
public String replace(CharSequence target,CharSequence replacement) {
return Pattern.compile(target.toString(),Pattern.LITERAL).matcher(
this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
