Android - Matcher.find() infinite

2019-03-04 03:53发布

问题:

I have implemented AsyncTask where the regular expression provided by the user is being used for matching huge html-code data. However, because some of the regular expressions contain a lot of quantifiers/backtracking, Matcher.find() goes infinite.

I have tried to use InterruptibleCharSequence provided here: How to terminate Matcher.find(), when its running too long?, but it seems that charAt is never get called, so never interrupted. My last guess is to create a new process just to run this regular matching procedure and then kill it when searching is cancelled. However, then I have a problem with sharing objects between Service and Activity, because of different processes.

回答1:

Parsing HTML using RegEx is a bad idea, see: Parsing Html The Cthulhu Way

What about parsing the DOM instead? I tried jsoup a while ago and found it pretty neat to work with.