/ / जावा में regex का उपयोग करके url खोजना और बदलना - java, regex, url, प्रतिस्थापित करना

जावा - जावा, रेगेक्स, यूआरएल, प्रतिस्थापन में regex का उपयोग कर एक यूआरएल ढूँढना और बदलना

मैं String.replace का उपयोग करके regex के साथ एक 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);
}
}

मैंने एसओ में इस पर कई प्रश्नों को देखा है लेकिन यह पैटर्न को प्रतिस्थापित नहीं करता है। क्या कोई मुझे बता सकता है कि मैं इसे कैसे प्राप्त करूं?

उत्तर:

जवाब के लिए 2 № 1

String.replace() एक नियमित अभिव्यक्ति को स्वीकार नहीं करता है। उपयोग String.replaceAll बजाय:

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

जहां तक ​​रेगेक्स का सवाल है, आपको मैच करना चाहिए https भी:

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

जवाब के लिए 2 № 2

आप के साथ एक regex का उपयोग नहीं कर सकते 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 “+&@#/%=~_|” «+&@#/%=~_|»