https://kotlinlang.org logo
#android
Title
# android
a

aidanvii

10/30/2019, 2:42 PM
To someone more experienced working with Regex patterns. I have a regex pattern that I use to sanitise bad json responses from our backend, replacing empty objects (
{}
) with null, and empty lists (
[]
) with null. The pattern is as follows:
Copy code
Regex("""\{}|\[]""")
This compiles and works just fine in a unit test, but on android it crashes with:
Copy code
Caused by: java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 3
    \{}|\[]
       ^
        at java.util.regex.Pattern.compileImpl(Native Method)
        at java.util.regex.Pattern.compile(Pattern.java:1344)
        at java.util.regex.Pattern.<init>(Pattern.java:1328)
        at java.util.regex.Pattern.compile(Pattern.java:950)
        at kotlin.text.Regex.<init>(Regex.kt:89)
All I can surmise from this is that the underlying native implementation on Android is different.
r

Rolando Lopez

10/30/2019, 2:50 PM
post an example of the text you’re trying to find over?
a

aidanvii

10/30/2019, 2:54 PM
Copy code
{ 
	"emptyObject" : {},
	"nonEmptyObject" : {
		"key" : "value"
	}
	"emptyList" : [],
	"nonEmptyList" : [
		"hello",
		"world"
	]
}
it fails when compiling the pattern, not when it processes the text
I've verified it works on standard JVM, but it's broken on Android
r

Rolando Lopez

10/30/2019, 3:04 PM
👍
3 Views