Using a string for a regexp pattern 
Author Message
 Using a string for a regexp pattern

The subject sort of sums it up. I want to be able to take a string
and see if it passes a given pattern. (positive test, none of this
funny negative stuff :)) The problem is the patterns are loaded
out of a database, hence only exist in string format.

The question is, am I missing something elegant in the language,
or is the only way to handle this is to generate a line of perl
code in a string then eval() it?

--
 Martin Terman, Mutant for Hire, Synchronicity Daemon, Priest of Shub-Internet
 Disclaimer: Sticks and stones may break my bones, but flames are just ignored

"Sig quotes are like bumper stickers, only without the same sense of relevance"



Mon, 24 Jun 1996 05:52:21 GMT  
 Using a string for a regexp pattern

:The subject sort of sums it up. I want to be able to take a string
:and see if it passes a given pattern. (positive test, none of this
:funny negative stuff :)) The problem is the patterns are loaded
:out of a database, hence only exist in string format.
:
:The question is, am I missing something elegant in the language,
:or is the only way to handle this is to generate a line of perl
:code in a string then eval() it?

    $foo =~ /$pat/

or if you're lucky ,  

    $foo =~ /$CONSTANT_PAT/o

But you really don't want to do it inside of this:


For that, clever evals are much much much better.

--tom
--

      "Will Hack Perl for Fine Food and Fun"
        Boulder Colorado  303-444-3212



Mon, 24 Jun 1996 09:44:24 GMT  
 Using a string for a regexp pattern
#>>    $foo =~ /$pat/
#>>
#>>or if you're lucky ,  
#>>
#>>    $foo =~ /$CONSTANT_PAT/o
#>>
#>>But you really don't want to do it inside of this:
#>>

#>>
#>For that, clever evals are much much much better.

Could you explain why? Don't you need the loop control?

Henry Manaster

--
***************************************************************************

        Brooklyn, NY            *
                                *
        Disclaimer: The above is not necessarily MY opinion nor that
                                of anyone else :-)  ????!
****************************************************************************



Tue, 25 Jun 1996 10:56:34 GMT  
 Using a string for a regexp pattern


:#>>    $foo =~ /$pat/
:#>>
:#>>or if you're lucky ,  
:#>>
:#>>    $foo =~ /$CONSTANT_PAT/o
:#>>
:#>>But you really don't want to do it inside of this:
:#>>

:#>>
:#>For that, clever evals are much much much better.
:
:Could you explain why? Don't you need the loop control?

Because if you do this

    while (<>) {

            if (/$pat/) { &something }
        }
    }

You will have a number of regexp compiles equal to the number of patterns
***MULTIPLIED*** by the number of strings your matching against!!  That means
hundred times more work on a 100-line file, a thousand times more work on
a 1000-line file, etc.

The rightish way to do this is:

    $code = "while (<>) {\n";

        $code .= "\tif (/$pat/) { &something }\n";
    }
    $code .= "}\n1;\n";

Or even:

    $code = <<END_OF_EVAL_CODE;
while (<>) {

        $code .= <<END_OF_EVAL_CODE
if (/$pat/) { &something }
END_OF_EVAL_CODE
    }
    $code .= <<END_OF_EVAL_CODE

Quote:
}

1;
END_OF_EVAL_CODE

I've long contemplating a multimatch library call that matched a
list of patterns against a list of string


    }

I'd have to work on being clever so as not to recompile the pats
if just the lines change.

--tom

--

      "Will Hack Perl for Fine Food and Fun"
        Boulder Colorado  303-444-3212



Wed, 26 Jun 1996 00:33:47 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. PERLFUNC: split - split up a string using a regexp delimiter

2. RegExp for the following string, w/o using split

3. PERLFUNC: split - split up a string using a regexp delimiter

4. ANN: String::Pattern - yet another pattern ( template ) processing module

5. Efficiency of some regexp patterns

6. Efficiency of some regexp patterns

7. Efficiency of some regexp patterns

8. regexp pattern matching

9. Matched pattern in regexp?

10. regexp pattern help

11. Binary pattern regexp

12. regexp, ONLY consist a given pattern

 

 
Powered by phpBB® Forum Software