Java supports regexes right? Nothing to it! that is so nice about it
Only after 15 years, I find out that it does have performance problems.
https://bugs.java.com/bugdatabase/view_bug.do?bug_id=5050507
Is a bug for regexes in java. Of course, they're rare. But how did I hit it?
Well, doing stuff you shouldn't of course. In this case, parsing Json, by searching for a value of a certain key.
That's straightforward, right? "key"\s:\s"([^"]+)"
Well, it would be nice like that, but there's this tiny exception... the value can contain quotes as well. Okay, so we exclude them. Numerous posts tell you how to do it. The trick is to use an or, in essence saying: 'the string \" OR any character which is not a "`
We can see that in action here
All languages support it.
but.... not java.
No, it has problems with these groups. It gives stack overflows. And it's a known fact.
So, is this the end?
I thought so. But then, I started digging a little deeper...
"key"\s:\s"((\\"|[^"])+)" Gives stack overflows... so what about
"key"\s:\s"((\\"|[^"]+)+)" that should be greedyer, maybe that helps? But it was too greedy.
"key"\s:\s"((\\"|[^"\\]+|\\)+)" Wait, what that actually works!?!?
So, our query then becomes: 'the string \" OR any string of characters which are not " or \ OR the string \`
Well, that makes a little bit of sense, but not a lot.
For now, we'll take it.
Reacties
Een reactie posten