开发者

Any perl standard library to check if a string contains a given substring

Given a query, I would like 开发者_高级运维to check if this contains a given substring (can contain more than one word) . But I don't want exhaustive search, because this substring can only start a fresh word.

Any perl standard libraries for this, so that I get something efficient and don't have to reinvent the wheel?

Thanks,


Maybe you'll find builtin index() suited for the job.

It's a very fast substring search function ( implements the Boyer-Moore algorithm ).

Just check its documentation with perldoc -f index.


I would make a hash with the key being the first word of the 9000 substrings and the value an array with all substrings with that first word. If many strings contain the same first word, you could use the first two words.

Then for each query, for each word, I would see if that word is in the hash, and then need to match only those strings in the hash's array, starting at that point in the string using the index function.

Assuming that matching is sparse, this would be pretty efficient. One hash lookup per word and minimal searching for potential matches.

As I write this it reminds me of an Aho-Corasick search. (See Algorithm::AhoCorasick in CPAN.) I've never used the module, but the algorithm spends a lot of time building a finite state machine out of the search keys so finding a match is super efficient. I don't know if the CPAN implementation handles word boundaries issues.


You can use this approach:

# init
my $re = join"|", map quotemeta, sort @substrings;
$re = qr/\b(?:$re)/;

# usage
while (<>) {
  found($1) if /($re)/;
}

where found is action what you want to do if substring found.


The builtin index function is the fastest general purpose way to check if a string contains a substring.

my $find = 'abc';

my $str = '123 abc xyz';

if (index($str, $find) != -1) {
    # process matching $str here
}

If index still is not fast enough, and you know where in the string your substring might be, you can narrow down on it using substr and then use eq for the actual comparison:

my $find = 'abc';

my $str = '123 abc xyz';

if (substr($str, 4, 3) eq $find) {
    # process matching $str here
}

You are not going to get faster than that in Perl without dropping down to C.


This sounds like the perfect job for regular expressions:

if($string =~ m/your substring/) { 
    say "substring found"; 
} else { 
    say "nothing found"; 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜