regex - Java dot doesn't match 'any character' -
dot should match character. why doesn't regex work?
string url = "http://wikipedia.org"; system.out.println(url.replace("htt.://", ""));
output: http://wikipedia.org
string.replace()
compiles literal regex. ought use string.replaceall()
or string.replacefirst()
:
string url = "http://wikipedia.org"; system.out.println(url.replacefirst("htt.://", ""));
here java source code, method .replace()
:
/** * replaces each substring of string matches literal target * sequence specified literal replacement sequence. * replacement proceeds beginning of string end, * example, replacing "aa" "b" in string "aaa" result in * "ba" rather "ab". * * @param target sequence of char values replaced * @param replacement replacement sequence of char values * @return resulting string * @throws nullpointerexception if <code>target</code> or * <code>replacement</code> <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())); }
Comments
Post a Comment