0

I am trying to replace a url with regex using String.replace and the code is below

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

I have looked into several questions on it in SO but it does not replace the pattern. Can someone please tell me how do i achieve that?

4
  • Your regex match google.com , make sure you change http? to https? Commented May 9, 2015 at 17:35
  • I don't see any replacement. You just want to remove google.com ? Commented May 9, 2015 at 17:38
  • @Pedro: am extremely sorry. I was caught up with work so that's why. Thanks a lot for your help. Won't repeat this next time anyways Commented May 9, 2015 at 19:15
  • NP, Apologies accepted, I'll remove my comment. Commented May 9, 2015 at 19:16

2 Answers 2

2

String.replace() does not accept a regular expression. Use String.replaceAll instead:

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

As far as the regex is concerned, you should match the https as well:

String regex = "(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
Sign up to request clarification or add additional context in comments.

Comments

2

You cannot use a regex with replace, use replaceAll instead, i.e.:

   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
    }

Output:

something  something

Live Demo:

http://ideone.com/Yi2hrb


Regex Explanation:

(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 “+&@#/%=~_|” «+&@#/%=~_|»

3 Comments

A tip: do not over-format and over-fill your posts with technical "junk". All the "try...catch" blocks in your posts make people believe you yourself doubt you suggested a working regex.
@stribizhev Thank you for the tip, but in java this is the correct way of handling errors.
However, your answer was not accepted... Not the first time. Thus, it is just my thought I wanted to share with you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.