I currently have a function with 3 regex in a row and I'm wondering if there's a way to compress it to a single one.
The steps:
1. I have a string ($data) that's joins user-submitted $name and $text
2. check to see if $data matches specific words or phrases, and if so then I remove those words so that they won't match later (mainly used when someone has a name like "dick cheney" that I don't want to match the pattern that's trying to filter "dick")
3. create a | delimited pattern from MySQL
4. check to see if $data matches that pattern
5. make an exception if $name is not a specific word / phrase OR the matched pattern's group name is not a specific word
The actual code:
$data = join(' ', $name, $text);
# Result: "foo this is foo's test"
if (
$name =~ m{^(
foo |
bar |
lorem |
ipsum
)$}x
) { $data =~ s/$1 ?//gi; }
# "foo" matches so remove it
# Result: "this is 's test"
($pattern) = $dbh->selectrow_array("SELECT GROUP_CONCAT(contains SEPARATOR '|') FROM tableA");
# Result: something like $pattern = "this|that|(?<TOT>the.other.thing)"
if ($data =~ /($pattern)/si &&
(
$name ne 'blah' ||
# don't let the (keys %+)[0] scare you, it's just the group name in the pattern match
(keys %+)[0] ne 'TOT'
)
) {
# do something
}
I tend to find double-negatives confusing to read, so for the sake of clarification the
if ($data =~ /($pattern)/si section could be more easily read as:
if (
$name eq 'blah' &&
(keys %+)[0] eq 'TOT'
) {
# do nothing
}
elsif ($data =~ /($pattern)/si) {
# do something
}
Can you suggest a way to do this without doing 3 separate regexes?