加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读