【问题标题】:Finding and replacing an url using regex in java在java中使用正则表达式查找和替换url
【发布时间】:2015-07-20 12:04:57
【问题描述】:

我正在尝试使用 String.replace 将 url 替换为正则表达式,代码如下

public class Test {
    public static void main(String[] args) {
        String test = "https://google.com";
        //String regex = "\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
        String regex = "(http?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"; // does not match <http://google.com>

        String newText = test.replace(regex, "");
        System.out.println(newText);
    }
}

我在 SO 中研究了几个关于它的问题,但它并没有取代模式。有人可以告诉我如何实现吗?

【问题讨论】:

  • 您的正则表达式匹配 google.com ,确保将 http? 更改为 https?
  • 我没有看到任何替代品。你只是想删除google.com
  • @Pedro:非常抱歉。我忙于工作,所以这就是原因。非常感谢你的帮助。反正下次不会重复了
  • NP,已接受道歉,我将删除我的评论。

标签: java regex url replace


【解决方案1】:

您不能将正则表达式与replace 一起使用,请改用replaceAll,即:

   String test = "something https://google.com something";
    try {
        String newText = test.replaceAll("(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]", "");
        System.out.println(newText);
    } catch (PatternSyntaxException ex) {
        // Syntax error in the regular expression
    } catch (IllegalArgumentException ex) {
        // Syntax error in the replacement text (unescaped $ signs?)
    } catch (IndexOutOfBoundsException ex) {
        // Non-existent backreference used the replacement text
    }

输出:

something  something

现场演示:

http://ideone.com/Yi2hrb


正则表达式解释:

(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]

Options: Case sensitive; Exact spacing; Dot doesn’t match line breaks; ^$ don’t match at line breaks; Default line breaks; Regex syntax only

Match the regex below and capture its match into backreference number 1 «(https?|ftp|file)»
   Match this alternative «https?»
      Match the character string “http” literally «http»
      Match the character “s” literally «s?»
         Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
   Or match this alternative «ftp»
      Match the character string “ftp” literally «ftp»
   Or match this alternative «file»
      Match the character string “file” literally «file»
Match the character string “://” literally «://»
Match a single character present in the list below «[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*»
   Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
   The literal character “-” «-»
   A character in the range between “a” and “z” «a-z»
   A character in the range between “A” and “Z” «A-Z»
   A character in the range between “0” and “9” «0-9»
   A single character from the list “+&@#/%?=~_|!:,.;” «+&@#/%?=~_|!:,.;»
Match a single character present in the list below «[-a-zA-Z0-9+&@#/%=~_|]»
   The literal character “-” «-»
   A character in the range between “a” and “z” «a-z»
   A character in the range between “A” and “Z” «A-Z»
   A character in the range between “0” and “9” «0-9»
   A single character from the list “+&@#/%=~_|” «+&@#/%=~_|»

【讨论】:

  • 提示:不要过度格式化和用技术“垃圾”填充您的帖子。您帖子中的所有“try...catch”块都让人们相信您自己怀疑您建议了一个有效的正则表达式。
  • @stribizhev 谢谢你的提示,但在java中这是处理错误的正确方法。
  • 但是,您的回答没有被接受...不是第一次。因此,这只是我想与您分享的想法。
【解决方案2】:

String.replace() 不接受正则表达式。请改用String.replaceAll

String newText = test.replaceAll(regex, "");

就正则表达式而言,您也应该匹配https

String regex = "(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";

【讨论】:

    猜你喜欢
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多