tags:

views:

104

answers:

3

In an Android app I have a utility class that I use to parse strings for 2 regEx's. I compile the 2 patterns in a static initializer so they only get compiled once, then activities can use the parsing methods statically.

This works fine except that the first time the class is accessed and loaded, and the static initializer compiles the pattern, the UI hangs for close to a MINUTE while it compiles the pattern! After the first time, it flies on all subsequent calls to parseString().

My regEx that I am using is rather large - 847 characters, but in a normal java webapp this is lightning fast. I am testing this so far only in the emulator with a 1.5 AVD.

Could this just be an emulator issue or is there some other reason that this pattern is taking so long to compile?

private static final String exp1 = "(insertratherlong---847character--regexhere)";
private static Pattern regex1 = null;

private static final String newLineAndTagsExp = "[<>\\s]";
private static Pattern regexNewLineAndTags = null;

static {
    regex1 = Pattern.compile(exp1, Pattern.CASE_INSENSITIVE);
    regexNewLineAndTags = Pattern.compile(newLineAndTagsExp);
}
public static String parseString(CharSequence inputStr) {

    String replacementStr = "replaceMentText";
    String resultString = "none";
    try {
        Matcher regexMatcher = regex1.matcher(inputStr);
        try {
            resultString = regexMatcher.replaceAll(replacementStr);
        } catch (IllegalArgumentException ex) {


        } catch (IndexOutOfBoundsException ex) {


        }
    } catch (PatternSyntaxException ex) {

    }

    return resultString;
}
A: 

your problem is in the regex pattern. not concentrate on other stuffs.

Praveen Chandrasekaran
A: 

If you launched with debugging you can expect it to be about twice as slow as a regular launch. However a minute does seem extraordinary. Some things to suggest, i. look at the console output to see if warnings are being spat out, ii. when it is doing the compile, in the debugger press 'pause' and just see what it is doing. There are ways to get the source, but even so just looking at the call stack may reveal something.

Jim Blackler
+1  A: 

please file a reproduceable test case at http://code.google.com/p/android/issues/entry and i'll have a look. note that i will need a regular expression that reproduces the problem. (our regular expressions are implemented by ICU4C, so the compilation actually happens in native code and this may end up being an ICU bug, but if you file an Android bug i'll worry about upstream.)

Elliott Hughes