pattern-matching question - Perl
This is a discussion on pattern-matching question - Perl ; I'm writing a quick-and-dirty editor to build SpamAssassin rules, and
thought it would be nice to put in a facility for testing the pattern
part of the rule against keyboard input. This works fine, except that
I haven't found a ...
-
pattern-matching question
I'm writing a quick-and-dirty editor to build SpamAssassin rules, and
thought it would be nice to put in a facility for testing the pattern
part of the rule against keyboard input. This works fine, except that
I haven't found a way to change the pattern modifiers (/i, /s, and /m
in particular; I can live without /x and /o) dynamically.
As an example, one rule might be written using the /i modifier, and
another using /s or /m. I can set up a case tree to match using the
appropriate fixed modifiers, but it would be _very_ much nicer and
more elegant to be able to build the pattern modifiers dynamically.
Am I, as usual, missing something simple and obvious?
TIA,
--
Mike Andrews, W5EGO
mikea@mikea.ath.cx
Tired old sysadmin
-
Re: pattern-matching question
mikea@mikea.ath.cx (Mike Andrews) writes:
> I'm writing a quick-and-dirty editor to build SpamAssassin rules, and
> thought it would be nice to put in a facility for testing the pattern
> part of the rule against keyboard input. This works fine, except that
> I haven't found a way to change the pattern modifiers (/i, /s, and /m
> in particular; I can live without /x and /o) dynamically.
Look for the "Extended Patterns" in the perlre manpage, especially the
part introduced by "(?imsx-imsx)". For example, /(?i)abc/ is about
same as /abc/i.
Torsten
BTW: I hope I understand your problem.
-
Re: pattern-matching question
Mike Andrews <mikea@mikea.ath.cx> wrote:
> I'm writing a quick-and-dirty editor to build SpamAssassin rules, and
> thought it would be nice to put in a facility for testing the pattern
> part of the rule against keyboard input. This works fine, except that
> I haven't found a way to change the pattern modifiers (/i, /s, and /m
> in particular; I can live without /x and /o) dynamically.
You can include the modifer in the regex. It's in the Extended Patterns
section of the perlre pod.
"(?imsx-imsx)"
One or more embedded pattern-match modifiers, to be turned on
(or turned off, if preceded by "-") for the remainder of the
pattern or the remainder of the enclosing pattern group (if
any). This is particularly useful for dynamic patterns, such
as those read in from a configuration file, read in as an
argument, are specified in a table somewhere, etc. Consider
the case that some of which want to be case sensitive and
some do not. The case insensitive ones need to include
merely "(?i)" at the front of the pattern. For example:
$pattern = "foobar";
if ( /$pattern/i ) { }
# more flexible:
$pattern = "(?i)foobar";
if ( /$pattern/ ) { }
--
Darren Dunham ddunham@taos.com
Senior Technical Consultant TAOS http://www.taos.com/
Got some Dr Pepper? San Francisco, CA bay area
< This line left intentionally blank to confuse you. >
Similar Threads
-
By Application Development in forum PHP
Replies: 3
Last Post: 12-03-2007, 07:24 AM
-
By Application Development in forum Functional
Replies: 7
Last Post: 06-29-2007, 10:19 PM
-
By Application Development in forum Perl
Replies: 3
Last Post: 05-03-2007, 01:02 PM
-
By Application Development in forum Compilers
Replies: 1
Last Post: 05-19-2005, 08:45 PM
-
By Application Development in forum Perl
Replies: 3
Last Post: 07-26-2003, 07:54 AM