Add check for circular search patterns

Before building the regular expression, convert search patterns into a graph and do a topological sort to
find circular patterns that would result in memory exhaustion when building the regex.