The regular expression, specifically for negative lookahead patterns, does not seem to work properly in Android 2.1 code.
See example below:
private String parseString(String regex, String raw) {
StringBuffer sb = new StringBuffer();
Matcher m = Pattern.compile(regex).matcher(raw);
m.matches();
if (m.find()) sb.append(m.group());
return sb.toString();
}
// Using the helper method above:
// Looking for 4-digit numeric strings within a text
String regex = "(\\d{4})(?!\\d)";
String text = "Looking for a 4-digit string 1234 in here!";
Log.i("Test", "[" + parseString(regex, text) + "]");
On Android 2.1, the result comes as:
I/Test ( 451): []
On Android 2.2, it is:
I/Test ( 451): [1234]
Does anyone know the reason for this?
There is a bug in Android's regular expression, specifically for negative lookahead patterns.
Official ticket was created and fixed in Android 2.2 (Froyo): http://code.google.com/p/android/issues/detail?id=17159
The solution:
(a) Do not use Android 2.1 if you need this; or
(b) Rebuild your regex without negative pattern (might get dirtier, but should work)