This may be dirty, but here We go...
Once you know that some string hitEnd
, do a second processing:
- Remove the last character from the string
- Search with the original regex
- If It
matches
, then you are over and you have the part of the string
- If not, go to 1 and repeat the whole process until you match
If test strings can be long, performance may be a problem. So instead of positions from last to first, try searching for blocks.
For example, considering a string of 1,000 chars:
- Test 1000/2 characters: 1-500. For this example, we consider it matches
- Test for first 500 chars + 500/2 (1-750 positions). For this example, We consider It does not match. So we know that the position must be placed from 500 to 750
- Now test 1-625 ((750+500)/2)... If it matches, the positions must exist between 625-750. If it does not match, It must be from 500 to 625
- ...