Refactored regex search to reduce code duplication