Use of Matcher.start

private String regexReplace(Pattern p, String text, int index,
        String replacement) {
    Matcher matcher = p.matcher(text);
    int counter = 0;
    while (matcher.find()) {
        if (counter == index) {
            int pos = matcher.start();
            String former = text.substring(0, pos);
            String later = text.substring(pos, text.length());
            Matcher m2 = p.matcher(later);
            return former + m2.replaceFirst(replacement);
        }
        counter++;
    }
    return null;
}

Let’s see how it work.

public static void main(String args[]) {
        Pattern p = Pattern.compile("\d");
        String candidateString = "1-2-3-4-6-7-8-9";
        System.out.println(regexReplace(p, candidateString, 0, "&"));
        System.out.println(regexReplace(p, candidateString, 3, "#"));
    }

And the result:

1-2-3-#-6-7-8-9
&-2-3-4-6-7-8-9

Operate remote shared file with Jcifs

Jcifs is a java package for manipulating file in shared folder of windows net work, in a way of operate common files in local machine.

1. Create an NtlmPasswordAuthentication object:

NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(String domainName, String login, String password);

If you are accessing share file in a work group, it’s even simpler:

NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(String login, String password);

2. Create SmbFile

SmbFile file = new SmbFile("smb://host/sharedFolder/someFile", auth)

3. Operator file with SmbFileInputStream or SmbFileOutputStream.


Upgrade Firefox on ubuntu

Unlike windows, Firefox doesn’t automatically upgrade itself, by default, on Ubuntu. Then you might want to manually upgrade from Help->"Check for updates…", but this option appears to be disabledSad
Well, that actually remind you that you are on a Ubuntu machine. Firefox is locate outside your home directory, and it make sense that you do not have write access to firefox, which result in the disable of upgrade. To overcome, simply start Firefox with "gksudo firefox", which make you run firefox as administrator. Then you will be happy to see the update is enabledSmile