Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.text.MessageFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static String regex = "http://[\\w\\.\\-/:]+";
    private static String A1 = " <a href={0}>";
    private static String A2 = " </a>";

    public static String toHref(String title) {
        StringBuffer sb = new StringBuffer(title);
        Pattern pat = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher mat = pat.matcher(title);
        int index = 0;
        int index1 = 0;
        while (mat.find()) {
            String url = mat.group();
            //System.out.println(url);
            if (url.indexOf("http://") != 0)
                url = "http://" + url;
            Object obj[] = { "'" + url + "'" };
            String a = MessageFormat.format(A1, obj);
            int l = a.length();
            index += index1;
            sb.insert(mat.start() + index, a);
            index += l;
            sb.insert((mat.end()) + index, A2);
            index1 = A2.length();
        }
        return sb.toString();
    }
}