Files
projectdiscovery-subfinder/pkg/subscraping/extractor.go
T
Mikel Olasagasti Uranga 6867e076f1 refactor: move v2 module code to root directory for cleaner structure
Keeps module path as github.com/projectdiscovery/subfinder/v2 to
preserve import compatibility.

Signed-off-by: Mikel Olasagasti Uranga <mikel@olasagasti.info>
2025-07-10 15:27:42 +02:00

31 lines
929 B
Go

package subscraping
import (
"regexp"
"strings"
)
// RegexSubdomainExtractor is a concrete implementation of the SubdomainExtractor interface, using regex for extraction.
type RegexSubdomainExtractor struct {
extractor *regexp.Regexp
}
// NewSubdomainExtractor creates a new regular expression to extract
// subdomains from text based on the given domain.
func NewSubdomainExtractor(domain string) (*RegexSubdomainExtractor, error) {
extractor, err := regexp.Compile(`(?i)[a-zA-Z0-9\*_.-]+\.` + domain)
if err != nil {
return nil, err
}
return &RegexSubdomainExtractor{extractor: extractor}, nil
}
// Extract implements the SubdomainExtractor interface, using the regex to find subdomains in the given text.
func (re *RegexSubdomainExtractor) Extract(text string) []string {
matches := re.extractor.FindAllString(text, -1)
for i, match := range matches {
matches[i] = strings.ToLower(match)
}
return matches
}