Android RegEx bug: ‘null’ replacement with Matcher.replaceAll
Contents
Android’s Regular Expressions engine is API compatible with the generic Java implementation, but under the hood uses ICU’s RegEx engine. This usually causes no problems, until it does and then you’re sortof fucked, but not really.
On a recent project, a subtle bug in Android’s Matcher.replaceAll
behavior bit me. When replacing values of captured groups ('$1'</code,<code>'$2'
, etc), Android’s implementation replaced a reference to an empty group with the literal string 'null'
instead of just skipping it (aka replacing with empty string ''
), which is what most other Java implemetations do. The string 'null'
is, I think, never the right behavior. And the inconsistency causes unit tests to pass when run on the desktop JRE to fail when run in Android, which is a major pain.
Fortunately Android is Open Source, and I was able to track down the offending piece of code, and just write myself a simple replacement that has the bug fixed:
You’re welcome. Yay Open Source!
Author yuvipanda
LastMod 2013-03-22