正規表現はまった・・・

Sabotterのコード書いててはまり中。


URL があったらリンクにするって処理があって、先頭のときだけリンクにならないってバグが。今は力業で解決した。

public class SabotterRegrexTest {
    public static void main(String... args) {
        String 先頭にhttp = "http://hoge.com/";
        String 途中にhttp = "test http://hoge.com/";
        String imgタグ的な何か = "test <img src=\"http://hoge.com/\"/>";

        System.out.println(convertLink(先頭にhttp));
        System.out.println(convertLink(途中にhttp));
        System.out.println(convertLink(imgタグ的な何か));
        
        
    }

    public static String convertLink(String source) {
        String htmlRegrex = "(http://|https://){1}[\\w\\.\\-/:\\\\&]+";

        source = source.replaceAll("[^\"^'^](" + htmlRegrex + ")", "<a href=\"$1\">$1</a>");
        return source;
    }
}

の実行結果が、今は

http://hoge.com/
test<a href="http://hoge.com/">http://hoge.com/</a>
test <img src="http://hoge.com/"/>

なのを

<a href="http://hoge.com/">http://hoge.com/</a>
test<a href="http://hoge.com/">http://hoge.com/</a>
test <img src="http://hoge.com/"/>

にする書き方がわからない><




追記。
コメントで教えてもらったとおり
source = source.replaceAll("([^\"^']|^)(" + htmlRegrex + ")", "$2");
でいけた。
ないの^は行頭を表さない。そりゃそうだね。内での^の意味の記号として扱われるんだから。