Fixed the code of GetPostsMatchingRegexPattern

The function didn't stop in certain situation and started from the
beginning on others.