Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: regex matching using arrays

by zejames (Hermit)
on Dec 04, 2004 at 10:58 UTC ( #412381=note: print w/replies, xml ) Need Help??


in reply to regex matching using arrays

Yes you can, by using logical OR in regex

use strict; use warnings; my @bah = ( "Australia", "Austria", "Finland", "Norway" ); my $regex = join ('|', map { "(?:$_)" } @bah); my $foo = "In Australia the people from Austria drink beer at the pub +with the people from Norway"; my $result = ""; my @data = $foo =~ /($regex)/g; print join ", ", @data;

HTH


--
zejames

Replies are listed 'Best First'.
Re^2: regex matching using arrays
by rsiedl (Friar) on Dec 04, 2004 at 11:08 UTC
    Cool, thanks zejames.
    Do you know if there is a limit on the size that $regex can take? i.e. if the array is really big will it break things?
      Your regex will extend up to the maximum size of your scalars.. which can go past the size of your physical memory! However, I'm sure after a few hundred OR clauses the regex will get bloody slow. You should probably bench it.
      Of course, that loops over the array twice (map and join)
      Do you know if there is a limit on the size that $regex can take? i.e. if the array is really big will it break things?
      If you have a large word list, and you're trying to determine which of those words occur in a given text of arbitrary length, the best approach is likely to be to store the word list in a hash, then tokenize the text string; for each tokenized word in the string, if that word exists as a hash key, you've got a hit (append that word to your output list).

      This way, you've got one simple loop over the "lexicon" (to store the word list as hash keys), and one loop over the word tokens in the text string; you're doing plain string comparisons (best to put everything in monocase) instead of regex matches, so it should be pretty quick.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://412381]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2020-10-29 03:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (268 votes). Check out past polls.

    Notices?