Server IP : 103.119.228.120 / Your IP : 18.227.0.255 Web Server : Apache System : Linux v8.techscape8.com 3.10.0-1160.119.1.el7.tuxcare.els2.x86_64 #1 SMP Mon Jul 15 12:09:18 UTC 2024 x86_64 User : nobody ( 99) PHP Version : 5.6.40 Disable Function : shell_exec,symlink,system,exec,proc_get_status,proc_nice,proc_terminate,define_syslog_variables,syslog,openlog,closelog,escapeshellcmd,passthru,ocinum cols,ini_alter,leak,listen,chgrp,apache_note,apache_setenv,debugger_on,debugger_off,ftp_exec,dl,dll,myshellexec,proc_open,socket_bind,proc_close,escapeshellarg,parse_ini_filepopen,fpassthru,exec,passthru,escapeshellarg,escapeshellcmd,proc_close,proc_open,ini_alter,popen,show_source,proc_nice,proc_terminate,proc_get_status,proc_close,pfsockopen,leak,apache_child_terminate,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,dl,symlink,shell_exec,system,dl,passthru,escapeshellarg,escapeshellcmd,myshellexec,c99_buff_prepare,c99_sess_put,fpassthru,getdisfunc,fx29exec,fx29exec2,is_windows,disp_freespace,fx29sh_getupdate,fx29_buff_prepare,fx29_sess_put,fx29shexit,fx29fsearch,fx29ftpbrutecheck,fx29sh_tools,fx29sh_about,milw0rm,imagez,sh_name,myshellexec,checkproxyhost,dosyayicek,c99_buff_prepare,c99_sess_put,c99getsource,c99sh_getupdate,c99fsearch,c99shexit,view_perms,posix_getpwuid,posix_getgrgid,posix_kill,parse_perms,parsesort,view_perms_color,set_encoder_input,ls_setcheckboxall,ls_reverse_all,rsg_read,rsg_glob,selfURL,dispsecinfo,unix2DosTime,addFile,system,get_users,view_size,DirFiles,DirFilesWide,DirPrintHTMLHeaders,GetFilesTotal,GetTitles,GetTimeTotal,GetMatchesCount,GetFileMatchesCount,GetResultFiles,fs_copy_dir,fs_copy_obj,fs_move_dir,fs_move_obj,fs_rmdir,SearchText,getmicrotime MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/local/ssl/local/ssl/local/ssl/local/ssl/local/share/man/man3/ |
Upload File : |
.\" Automatically generated by Pod::Man 2.27 (Pod::Simple 3.28) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Regexp::Assemble 3" .TH Regexp::Assemble 3 "2017-06-20" "perl v5.16.3" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Regexp::Assemble \- Assemble multiple Regular Expressions into a single RE .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Regexp::Assemble; \& \& my $ra = Regexp::Assemble\->new; \& $ra\->add( \*(Aqab+c\*(Aq ); \& $ra\->add( \*(Aqab+\-\*(Aq ); \& $ra\->add( \*(Aqa\ew\ed+\*(Aq ); \& $ra\->add( \*(Aqa\ed+\*(Aq ); \& print $ra\->re; # prints a(?:\ew?\ed+|b+[\-c]) .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Regexp::Assemble takes an arbitrary number of regular expressions and assembles them into a single regular expression (or \s-1RE\s0) that matches all that the individual REs match. .PP As a result, instead of having a large list of expressions to loop over, a target string only needs to be tested against one expression. This is interesting when you have several thousand patterns to deal with. Serious effort is made to produce the smallest pattern possible. .PP It is also possible to track the original patterns, so that you can determine which, among the source patterns that form the assembled pattern, was the one that caused the match to occur. .PP You should realise that large numbers of alternations are processed in perl's regular expression engine in O(n) time, not O(1). If you are still having performance problems, you should look at using a trie. Note that Perl's own regular expression engine will implement trie optimisations in perl 5.10 (they are already available in perl 5.9.3 if you want to try them out). \f(CW\*(C`Regexp::Assemble\*(C'\fR will do the right thing when it knows it's running on a trie'd perl. (At least in some version after this one). .PP Some more examples of usage appear in the accompanying \s-1README.\s0 If that file is not easy to access locally, you can find it on a web repository such as <http://search.cpan.org/dist/Regexp\-Assemble/README> or <http://cpan.uwinnipeg.ca/htdocs/Regexp\-Assemble/README.html>. .PP See also \*(L"\s-1LIMITATIONS\*(R"\s0. .SH "Methods" .IX Header "Methods" .SS "add(\s-1LIST\s0)" .IX Subsection "add(LIST)" Takes a string, breaks it apart into a set of tokens (respecting meta characters) and inserts the resulting list into the \f(CW\*(C`R::A\*(C'\fR object. It uses a naive regular expression to lex the string that may be fooled complex expressions (specifically, it will fail to lex nested parenthetical expressions such as \&\f(CW\*(C`ab(cd(ef)?gh)ij\*(C'\fR correctly). If this is the case, the end of the string will not be tokenised correctly and returned as one long string. .PP On the one hand, this may indicate that the patterns you are trying to feed the \f(CW\*(C`R::A\*(C'\fR object are too complex. Simpler patterns might allow the algorithm to work more effectively and perform more reductions in the resulting pattern. .PP On the other hand, you can supply your own pattern to perform the lexing if you need. The test suite contains an example of a lexer pattern that will match one level of nested parentheses. .PP Note that there is an internal optimisation that will bypass a much of the lexing process. If a string contains no \f(CW\*(C`\e\*(C'\fR (backslash), \f(CW\*(C`[\*(C'\fR (open square bracket), \f(CW\*(C`(\*(C'\fR (open paren), \&\f(CW\*(C`?\*(C'\fR (question mark), \f(CW\*(C`+\*(C'\fR (plus), \f(CW\*(C`*\*(C'\fR (star) or \f(CW\*(C`{\*(C'\fR (open curly), a character split will be performed directly. .PP A list of strings may be supplied, thus you can pass it a file handle of a file opened for reading: .PP .Vb 2 \& $re\->add( \*(Aq\ed+\-\ed+\-\ed+\-\ed+\e.example\e.com\*(Aq ); \& $re\->add( <IN> ); .Ve .PP If the file is very large, it may be more efficient to use a \&\f(CW\*(C`while\*(C'\fR loop, to read the file line-by-line: .PP .Vb 1 \& $re\->add($_) while <IN>; .Ve .PP The \f(CW\*(C`add\*(C'\fR method will chomp the lines automatically. If you do not want this to occur (you want to keep the record separator), then disable \f(CW\*(C`chomp\*(C'\fRing. .PP .Vb 2 \& $re\->chomp(0); \& $re\->add($_) while <IN>; .Ve .PP This method is chainable. .SS "add_file(\s-1FILENAME\s0 [...])" .IX Subsection "add_file(FILENAME [...])" Takes a list of file names. Each file is opened and read line by line. Each line is added to the assembly. .PP .Vb 1 \& $r\->add_file( \*(Aqfile.1\*(Aq, \*(Aqfile.2\*(Aq ); .Ve .PP If a file cannot be opened, the method will croak. If you cannot afford to let this happen then you should wrap the call in a \f(CW\*(C`eval\*(C'\fR block. .PP Chomping happens automatically unless you the \f(CWchomp(0)\fR method to disable it. By default, input lines are read according to the value of the \f(CW\*(C`input_record_separator\*(C'\fR attribute (if defined), and will otherwise fall back to the current setting of the system \f(CW$/\fR variable. The record separator may also be specified on each call to \f(CW\*(C`add_file\*(C'\fR. Internally, the routine \f(CW\*(C`local\*(C'\fRises the value of \f(CW$/\fR to whatever is required, for the duration of the call. .PP An alternate calling mechanism using a hash reference is available. The recognised keys are: .IP "file" 4 .IX Item "file" Reference to a list of file names, or the name of a single file. .Sp .Vb 2 \& $r\->add_file({file => [\*(Aqfile.1\*(Aq, \*(Aqfile.2\*(Aq, \*(Aqfile.3\*(Aq]}); \& $r\->add_file({file => \*(Aqfile.n\*(Aq}); .Ve .IP "input_record_separator" 4 .IX Item "input_record_separator" If present, indicates what constitutes a line .Sp .Vb 1 \& $r\->add_file({file => \*(Aqdata.txt\*(Aq, input_record_separator => \*(Aq:\*(Aq }); .Ve .IP "rs" 4 .IX Item "rs" An alias for input_record_separator (mnemonic: same as the English variable names). .PP .Vb 4 \& $r\->add_file( { \& file => [ \*(Aqpattern.txt\*(Aq, \*(Aqmore.txt\*(Aq ], \& input_record_separator => "\er\en", \& }); .Ve .SS "\fIclone()\fP" .IX Subsection "clone()" Clones the contents of a Regexp::Assemble object and creates a new object (in other words it performs a deep copy). .PP If the Storable module is installed, its dclone method will be used, otherwise the cloning will be performed using a pure perl approach. .PP You can use this method to take a snapshot of the patterns that have been added so far to an object, and generate an assembly from the clone. Additional patterns may to be added to the original object afterwards. .PP .Vb 2 \& my $re = $main\->clone\->re(); \& $main\->add( \*(Aqanother\-pattern\-\e\ed+\*(Aq ); .Ve .SS "insert(\s-1LIST\s0)" .IX Subsection "insert(LIST)" Takes a list of tokens representing a regular expression and stores them in the object. Note: you should not pass it a bare regular expression, such as \f(CW\*(C`ab+c?d*e\*(C'\fR. You must pass it as a list of tokens, \fIe.g.\fR \f(CW\*(C`(\*(Aqa\*(Aq, \*(Aqb+\*(Aq, \*(Aqc?\*(Aq, \*(Aqd*\*(Aq, \*(Aqe\*(Aq)\*(C'\fR. .PP This method is chainable, \fIe.g.\fR: .PP .Vb 3 \& my $ra = Regexp::Assemble\->new \& \->insert( qw[ a b+ c? d* e ] ) \& \->insert( qw[ a c+ d+ e* f ] ); .Ve .PP Lexing complex patterns with metacharacters and so on can consume a significant proportion of the overall time to build an assembly. If you have the information available in a tokenised form, calling \&\f(CW\*(C`insert\*(C'\fR directly can be a big win. .SS "lexstr" .IX Subsection "lexstr" Use the \f(CW\*(C`lexstr\*(C'\fR method if you are curious to see how a pattern gets tokenised. It takes a scalar on input, representing a pattern, and returns a reference to an array, containing the tokenised pattern. You can recover the original pattern by performing a \&\f(CW\*(C`join\*(C'\fR: .PP .Vb 2 \& my @token = $re\->lexstr($pattern); \& my $new_pattern = join( \*(Aq\*(Aq, @token ); .Ve .PP If the original pattern contains unnecessary backslashes, or \f(CW\*(C`\ex4b\*(C'\fR escapes, or quotemeta escapes (\f(CW\*(C`\eQ\*(C'\fR...\f(CW\*(C`\eE\*(C'\fR) the resulting pattern may not be identical. .PP Call \f(CW\*(C`lexstr\*(C'\fR does not add the pattern to the object, it is merely for exploratory purposes. It will, however, update various statistical counters. .SS "pre_filter(\s-1CODE\s0)" .IX Subsection "pre_filter(CODE)" Allows you to install a callback to check that the pattern being loaded contains valid input. It receives the pattern as a whole to be added, before it been tokenised by the lexer. It may to return 0 or \f(CW\*(C`undef\*(C'\fR to indicate that the pattern should not be added, any true value indicates that the contents are fine. .PP A filter to strip out trailing comments (marked by #): .PP .Vb 1 \& $re\->pre_filter( sub { $_[0] =~ s/\es*#.*$//; 1 } ); .Ve .PP A filter to ignore blank lines: .PP .Vb 1 \& $re\->pre_filter( sub { length(shift) } ); .Ve .PP If you want to remove the filter, pass \f(CW\*(C`undef\*(C'\fR as a parameter. .PP .Vb 1 \& $ra\->pre_filter(undef); .Ve .PP This method is chainable. .SS "filter(\s-1CODE\s0)" .IX Subsection "filter(CODE)" Allows you to install a callback to check that the pattern being loaded contains valid input. It receives a list on input, after it has been tokenised by the lexer. It may to return 0 or undef to indicate that the pattern should not be added, any true value indicates that the contents are fine. .PP If you know that all patterns you expect to assemble contain a restricted set of of tokens (e.g. no spaces), you could do the following: .PP .Vb 1 \& $ra\->filter(sub { not grep { / / } @_ }); .Ve .PP or .PP .Vb 4 \& sub only_spaces_and_digits { \& not grep { ![\ed ] } @_ \& } \& $ra\->filter( \e&only_spaces_and_digits ); .Ve .PP These two examples will silently ignore faulty patterns, If you want the user to be made aware of the problem you should raise an error (via \f(CW\*(C`warn\*(C'\fR or \f(CW\*(C`die\*(C'\fR), log an error message, whatever is best. If you want to remove a filter, pass \f(CW\*(C`undef\*(C'\fR as a parameter. .PP .Vb 1 \& $ra\->filter(undef); .Ve .PP This method is chainable. .SS "as_string" .IX Subsection "as_string" Assemble the expression and return it as a string. You may want to do this if you are writing the pattern to a file. The following arguments can be passed to control the aspect of the resulting pattern: .PP \&\fBindent\fR, the number of spaces used to indent nested grouping of a pattern. Use this to produce a pretty-printed pattern (for some definition of \*(L"pretty\*(R"). The resulting output is rather verbose. The reason is to ensure that the metacharacters \f(CW\*(C`(?:\*(C'\fR and \f(CW\*(C`)\*(C'\fR always occur on otherwise empty lines. This allows you grep the result for an even more synthetic view of the pattern: .PP .Vb 1 \& egrep \-v \*(Aq^ *[()]\*(Aq <regexp.file> .Ve .PP The result of the above is quite readable. Remember to backslash the spaces appearing in your own patterns if you wish to use an indented pattern in an \f(CW\*(C`m/.../x\*(C'\fR construct. Indenting is ignored if tracking is enabled. .PP The \fBindent\fR argument takes precedence over the \f(CW\*(C`indent\*(C'\fR method/attribute of the object. .PP Calling this method will drain the internal data structure. Large numbers of patterns can eat a significant amount of memory, and this lets perl recover the memory used for other purposes. .PP If you want to reduce the pattern \fIand\fR continue to add new patterns, clone the object and reduce the clone, leaving the original object intact. .SS "re" .IX Subsection "re" Assembles the pattern and return it as a compiled \s-1RE,\s0 using the \&\f(CW\*(C`qr//\*(C'\fR operator. .PP As with \f(CW\*(C`as_string\*(C'\fR, calling this method will reset the internal data structures to free the memory used in assembling the \s-1RE.\s0 .PP The \fBindent\fR attribute, documented in the \f(CW\*(C`as_string\*(C'\fR method, can be used here (it will be ignored if tracking is enabled). .PP With method chaining, it is possible to produce a \s-1RE\s0 without having a temporary \f(CW\*(C`Regexp::Assemble\*(C'\fR object lying around, \fIe.g.\fR: .PP .Vb 5 \& my $re = Regexp::Assemble\->new \& \->add( q[ab+cd+e] ) \& \->add( q[ac\e\ed+e] ) \& \->add( q[c\e\ed+e] ) \& \->re; .Ve .PP The \f(CW$re\fR variable now contains a Regexp object that can be used directly: .PP .Vb 3 \& while( <> ) { \& /$re/ and print "Something in [$_] matched\en"; \& ) .Ve .PP The \f(CW\*(C`re\*(C'\fR method is called when the object is used in string context (hence, within an \f(CW\*(C`m//\*(C'\fR operator), so by and large you do not even need to save the \s-1RE\s0 in a separate variable. The following will work as expected: .PP .Vb 6 \& my $re = Regexp::Assemble\->new\->add( qw[ fee fie foe fum ] ); \& while( <IN> ) { \& if( /($re)/ ) { \& print "Here be giants: $1\en"; \& } \& } .Ve .PP This approach does not work with tracked patterns. The \&\f(CW\*(C`match\*(C'\fR and \f(CW\*(C`matched\*(C'\fR methods must be used instead, see below. .SS "match(\s-1SCALAR\s0)" .IX Subsection "match(SCALAR)" The following information applies to Perl 5.8 and below. See the section that follows for information on Perl 5.10. .PP If pattern tracking is in use, you must \f(CW\*(C`use re \*(Aqeval\*(Aq\*(C'\fR in order to make things work correctly. At a minimum, this will make your code look like this: .PP .Vb 4 \& my $did_match = do { use re \*(Aqeval\*(Aq; $target =~ /$ra/ } \& if( $did_match ) { \& print "matched ", $ra\->matched, "\en"; \& } .Ve .PP (The main reason is that the \f(CW$^R\fR variable is currently broken and an ugly workaround that runs some Perl code during the match is required, in order to simulate what \f(CW$^R\fR should be doing. See Perl bug #32840 for more information if you are curious. The \s-1README\s0 also contains more information). This bug has been fixed in 5.10. .PP The important thing to note is that with \f(CW\*(C`use re \*(Aqeval\*(Aq\*(C'\fR, \s-1THERE ARE SECURITY IMPLICATIONS WHICH YOU IGNORE AT YOUR PERIL.\s0 The problem is this: if you do not have strict control over the patterns being fed to \f(CW\*(C`Regexp::Assemble\*(C'\fR when tracking is enabled, and someone slips you a pattern such as \f(CW\*(C`/^(?{system \*(Aqrm \-rf /\*(Aq})/\*(C'\fR and you attempt to match a string against the resulting pattern, you will know Fear and Loathing. .PP What is more, the \f(CW$^R\fR workaround means that that tracking does not work if you perform a bare \f(CW\*(C`/$re/\*(C'\fR pattern match as shown above. You have to instead call the \f(CW\*(C`match\*(C'\fR method, in order to supply the necessary context to take care of the tracking housekeeping details. .PP .Vb 3 \& if( defined( my $match = $ra\->match($_)) ) { \& print " $_ matched by $match\en"; \& } .Ve .PP In the case of a successful match, the original matched pattern is returned directly. The matched pattern will also be available through the \f(CW\*(C`matched\*(C'\fR method. .PP (Except that the above is not true for 5.6.0: the \f(CW\*(C`match\*(C'\fR method returns true or undef, and the \f(CW\*(C`matched\*(C'\fR method always returns undef). .PP If you are capturing parts of the pattern \fIe.g.\fR \f(CW\*(C`foo(bar)rat\*(C'\fR you will want to get at the captures. See the \f(CW\*(C`mbegin\*(C'\fR, \f(CW\*(C`mend\*(C'\fR, \&\f(CW\*(C`mvar\*(C'\fR and \f(CW\*(C`capture\*(C'\fR methods. If you are not using captures then you may safely ignore this section. .PP In 5.10, since the bug concerning \f(CW$^R\fR has been resolved, there is no need to use \f(CW\*(C`re \*(Aqeval\*(Aq\*(C'\fR and the assembled pattern does not require any Perl code to be executed during the match. .SS "\fInew()\fP" .IX Subsection "new()" Creates a new \f(CW\*(C`Regexp::Assemble\*(C'\fR object. The following optional key/value parameters may be employed. All keys have a corresponding method that can be used to change the behaviour later on. As a general rule, especially if you're just starting out, you don't have to bother with any of these. .PP \&\fBanchor_*\fR, a family of optional attributes that allow anchors (\f(CW\*(C`^\*(C'\fR, \f(CW\*(C`\eb\*(C'\fR, \f(CW\*(C`\eZ\*(C'\fR...) to be added to the resulting pattern. .PP \&\fBflags\fR, sets the \f(CW\*(C`imsx\*(C'\fR flags to add to the assembled regular expression. Warning: no error checking is done, you should ensure that the flags you pass are understood by the version of Perl you are using. \fBmodifiers\fR exists as an alias, for users familiar with Regexp::List. .PP \&\fBchomp\fR, controls whether the pattern should be chomped before being lexed. Handy if you are reading patterns from a file. By default, \&\f(CW\*(C`chomp\*(C'\fRing is performed (this behaviour changed as of version 0.24, prior versions did not chomp automatically). See also the \f(CW\*(C`file\*(C'\fR attribute and the \f(CW\*(C`add_file\*(C'\fR method. .PP \&\fBfile\fR, slurp the contents of the specified file and add them to the assembly. Multiple files may be processed by using a list. .PP .Vb 1 \& my $r = Regexp::Assemble\->new(file => \*(Aqre.list\*(Aq); \& \& my $r = Regexp::Assemble\->new(file => [\*(Aqre.1\*(Aq, \*(Aqre.2\*(Aq]); .Ve .PP If you really don't want chomping to occur, you will have to set the \f(CW\*(C`chomp\*(C'\fR attribute to 0 (zero). You may also want to look at the \f(CW\*(C`input_record_separator\*(C'\fR attribute, as well. .PP \&\fBinput_record_separator\fR, controls what constitutes a record separator when using the \f(CW\*(C`file\*(C'\fR attribute or the \f(CW\*(C`add_file\*(C'\fR method. May be abbreviated to \fBrs\fR. See the \f(CW$/\fR variable in perlvar. .PP \&\fBlookahead\fR, controls whether the pattern should contain zero-width lookahead assertions (For instance: (?=[abc])(?:bob|alice|charles). This is not activated by default, because in many circumstances the cost of processing the assertion itself outweighs the benefit of its faculty for short-circuiting a match that will fail. This is sensitive to the probability of a match succeeding, so if you're worried about performance you'll have to benchmark a sample population of targets to see which way the benefits lie. .PP \&\fBtrack\fR, controls whether you want know which of the initial patterns was the one that matched. See the \f(CW\*(C`matched\*(C'\fR method for more details. Note for version 5.8 of Perl and below, in this mode of operation \s-1YOU SHOULD BE AWARE OF THE SECURITY IMPLICATIONS\s0 that this entails. Perl 5.10 does not suffer from any such restriction. .PP \&\fBindent\fR, the number of spaces used to indent nested grouping of a pattern. Use this to produce a pretty-printed pattern. See the \&\f(CW\*(C`as_string\*(C'\fR method for a more detailed explanation. .PP \&\fBpre_filter\fR, allows you to add a callback to enable sanity checks on the pattern being loaded. This callback is triggered before the pattern is split apart by the lexer. In other words, it operates on the entire pattern. If you are loading patterns from a file, this would be an appropriate place to remove comments. .PP \&\fBfilter\fR, allows you to add a callback to enable sanity checks on the pattern being loaded. This callback is triggered after the pattern has been split apart by the lexer. .PP \&\fBunroll_plus\fR, controls whether to unroll, for example, \f(CW\*(C`x+\*(C'\fR into \&\f(CW\*(C`x\*(C'\fR, \f(CW\*(C`x*\*(C'\fR, which may allow additional reductions in the resulting assembled pattern. .PP \&\fBreduce\fR, controls whether tail reduction occurs or not. If set, patterns like \f(CW\*(C`a(?:bc+d|ec+d)\*(C'\fR will be reduced to \f(CW\*(C`a[be]c+d\*(C'\fR. That is, the end of the pattern in each part of the b... and d... alternations is identical, and hence is hoisted out of the alternation and placed after it. On by default. Turn it off if you're really pressed for short assembly times. .PP \&\fBlex\fR, specifies the pattern used to lex the input lines into tokens. You could replace the default pattern by a more sophisticated version that matches arbitrarily nested parentheses, for example. .PP \&\fBdebug\fR, controls whether copious amounts of output is produced during the loading stage or the reducing stage of assembly. .PP .Vb 2 \& my $ra = Regexp::Assemble\->new; \& my $rb = Regexp::Assemble\->new( chomp => 1, debug => 3 ); .Ve .PP \&\fBmutable\fR, controls whether new patterns can be added to the object after the assembled pattern is generated. \s-1DEPRECATED.\s0 .PP This method/attribute will be removed in a future release. It doesn't really serve any purpose, and may be more effectively replaced by cloning an existing \f(CW\*(C`Regexp::Assemble\*(C'\fR object and spinning out a pattern from that instead. .SS "\fIsource()\fP" .IX Subsection "source()" When using tracked mode, after a successful match is made, returns the original source pattern that caused the match. In Perl 5.10, the \f(CW$^R\fR variable can be used to as an index to fetch the correct pattern from the object. .PP If no successful match has been performed, or the object is not in tracked mode, this method returns \f(CW\*(C`undef\*(C'\fR. .PP .Vb 1 \& my $r = Regexp::Assemble\->new\->track(1)\->add(qw(foo? bar{2} [Rr]at)); \& \& for my $w (qw(this food is rather barren)) { \& if ($w =~ /$r/) { \& print "$w matched by ", $r\->source($^R), $/; \& } \& else { \& print "$w no match\en"; \& } \& } .Ve .SS "\fImbegin()\fP" .IX Subsection "mbegin()" This method returns a copy of \f(CW\*(C`@\-\*(C'\fR at the moment of the last match. You should ordinarily not need to bother with this, \f(CW\*(C`mvar\*(C'\fR should be able to supply all your needs. .SS "\fImend()\fP" .IX Subsection "mend()" This method returns a copy of \f(CW\*(C`@+\*(C'\fR at the moment of the last match. .SS "mvar(\s-1NUMBER\s0)" .IX Subsection "mvar(NUMBER)" The \f(CW\*(C`mvar\*(C'\fR method returns the captures of the last match. \&\f(CWmvar(1)\fR corresponds to \f(CW$1\fR, \f(CWmvar(2)\fR to \f(CW$2\fR, and so on. \&\f(CWmvar(0)\fR happens to return the target string matched, as a byproduct of walking down the \f(CW\*(C`@\-\*(C'\fR and \f(CW\*(C`@+\*(C'\fR arrays after the match. .PP If called without a parameter, \f(CW\*(C`mvar\*(C'\fR will return a reference to an array containing all captures. .SS "capture" .IX Subsection "capture" The \f(CW\*(C`capture\*(C'\fR method returns the the captures of the last match as an array. Unlink \f(CW\*(C`mvar\*(C'\fR, this method does not include the matched string. It is equivalent to getting an array back that contains \f(CW\*(C`$1, $2, $3, ...\*(C'\fR. .PP If no captures were found in the match, an empty array is returned, rather than \f(CW\*(C`undef\*(C'\fR. You are therefore guaranteed to be able to use \f(CW\*(C`for my $c ($re\->capture) { ...\*(C'\fR without have to check whether anything was captured. .SS "\fImatched()\fP" .IX Subsection "matched()" If pattern tracking has been set, via the \f(CW\*(C`track\*(C'\fR attribute, or through the \f(CW\*(C`track\*(C'\fR method, this method will return the original pattern of the last successful match. Returns undef match has yet been performed, or tracking has not been enabled. .PP See below in the \s-1NOTES\s0 section for additional subtleties of which you should be aware of when tracking patterns. .PP Note that this method is not available in 5.6.0, due to limitations in the implementation of \f(CW\*(C`(?{...})\*(C'\fR at the time. .SS "Statistics/Reporting routines" .IX Subsection "Statistics/Reporting routines" .SS "stats_add" .IX Subsection "stats_add" Returns the number of patterns added to the assembly (whether by \f(CW\*(C`add\*(C'\fR or \f(CW\*(C`insert\*(C'\fR). Duplicate patterns are not included in this total. .SS "stats_dup" .IX Subsection "stats_dup" Returns the number of duplicate patterns added to the assembly. If non-zero, this may be a sign that something is wrong with your data (or at the least, some needless redundancy). This may occur when you have two patterns (for instance, \f(CW\*(C`a\e\-b\*(C'\fR and \&\f(CW\*(C`a\-b\*(C'\fR) which map to the same result. .SS "\fIstats_raw()\fP" .IX Subsection "stats_raw()" Returns the raw number of bytes in the patterns added to the assembly. This includes both original and duplicate patterns. For instance, adding the two patterns \f(CW\*(C`ab\*(C'\fR and \f(CW\*(C`ab\*(C'\fR will count as 4 bytes. .SS "\fIstats_cooked()\fP" .IX Subsection "stats_cooked()" Return the true number of bytes added to the assembly. This will not include duplicate patterns. Furthermore, it may differ from the raw bytes due to quotemeta treatment. For instance, \&\f(CW\*(C`abc\e,def\*(C'\fR will count as 7 (not 8) bytes, because \f(CW\*(C`\e,\*(C'\fR will be stored as \f(CW\*(C`,\*(C'\fR. Also, \f(CW\*(C`\eQa.b\eE\*(C'\fR is 7 bytes long, however, after the quotemeta directives are processed, \f(CW\*(C`a\e.b\*(C'\fR will be stored, for a total of 4 bytes. .SS "\fIstats_length()\fP" .IX Subsection "stats_length()" Returns the length of the resulting assembled expression. Until \f(CW\*(C`as_string\*(C'\fR or \f(CW\*(C`re\*(C'\fR have been called, the length will be 0 (since the assembly will have not yet been performed). The length includes only the pattern, not the additional (\f(CW\*(C`(?\-xism...\*(C'\fR) fluff added by the compilation. .SS "dup_warn(NUMBER|CODEREF)" .IX Subsection "dup_warn(NUMBER|CODEREF)" Turns warnings about duplicate patterns on or off. By default, no warnings are emitted. If the method is called with no parameters, or a true parameter, the object will carp about patterns it has already seen. To turn off the warnings, use 0 as a parameter. .PP .Vb 1 \& $r\->dup_warn(); .Ve .PP The method may also be passed a code block. In this case the code will be executed and it will receive a reference to the object in question, and the lexed pattern. .PP .Vb 7 \& $r\->dup_warn( \& sub { \& my $self = shift; \& print $self\->stats_add, " patterns added at line $.\en", \& join( \*(Aq\*(Aq, @_ ), " added previously\en"; \& } \& ) .Ve .SS "Anchor routines" .IX Subsection "Anchor routines" Suppose you wish to assemble a series of patterns that all begin with \f(CW\*(C`^\*(C'\fR and end with \f(CW\*(C`$\*(C'\fR (anchor pattern to the beginning and end of line). Rather than add the anchors to each and every pattern (and possibly forget to do so when a new entry is added), you may specify the anchors in the object, and they will appear in the resulting pattern, and you no longer need to (or should) put them in your source patterns. For example, the two following snippets will produce identical patterns: .PP .Vb 1 \& $r\->add(qw(^this ^that ^them))\->as_string; \& \& $r\->add(qw(this that them))\->anchor_line_begin\->as_string; \& \& # both techniques will produce ^th(?:at|em|is) .Ve .PP All anchors are possible word (\f(CW\*(C`\eb\*(C'\fR) boundaries, line boundaries (\f(CW\*(C`^\*(C'\fR and \f(CW\*(C`$\*(C'\fR) and string boundaries (\f(CW\*(C`\eA\*(C'\fR and \f(CW\*(C`\eZ\*(C'\fR (or \f(CW\*(C`\ez\*(C'\fR if you absolutely need it)). .PP The shortcut \f(CW\*(C`anchor_\f(CImumble\f(CW\*(C'\fR implies both \&\f(CW\*(C`anchor_\f(CImumble\f(CW_begin\*(C'\fR \f(CW\*(C`anchor_\f(CImumble\f(CW_end\*(C'\fR is also available. If different anchors are specified the most specific anchor wins. For instance, if both \&\f(CW\*(C`anchor_word_begin\*(C'\fR and \f(CW\*(C`anchor_line_begin\*(C'\fR are specified, \f(CW\*(C`anchor_word_begin\*(C'\fR takes precedence. .PP All the anchor methods are chainable. .SS "anchor_word_begin" .IX Subsection "anchor_word_begin" The resulting pattern will be prefixed with a \f(CW\*(C`\eb\*(C'\fR word boundary assertion when the value is true. Set to 0 to disable. .PP .Vb 2 \& $r\->add(\*(Aqpre\*(Aq)\->anchor_word_begin\->as_string; \& # produces \*(Aq\ebpre\*(Aq .Ve .SS "anchor_word_end" .IX Subsection "anchor_word_end" The resulting pattern will be suffixed with a \f(CW\*(C`\eb\*(C'\fR word boundary assertion when the value is true. Set to 0 to disable. .PP .Vb 3 \& $r\->add(qw(ing tion)) \& \->anchor_word_end \& \->as_string; # produces \*(Aq(?:tion|ing)\eb\*(Aq .Ve .SS "anchor_word" .IX Subsection "anchor_word" The resulting pattern will be have \f(CW\*(C`\eb\*(C'\fR word boundary assertions at the beginning and end of the pattern when the value is true. Set to 0 to disable. .PP .Vb 3 \& $r\->add(qw(cat carrot) \& \->anchor_word(1) \& \->as_string; # produces \*(Aq\ebca(?:rro)t\eb\*(Aq .Ve .SS "anchor_line_begin" .IX Subsection "anchor_line_begin" The resulting pattern will be prefixed with a \f(CW\*(C`^\*(C'\fR line boundary assertion when the value is true. Set to 0 to disable. .PP .Vb 3 \& $r\->anchor_line_begin; \& # or \& $r\->anchor_line_begin(1); .Ve .SS "anchor_line_end" .IX Subsection "anchor_line_end" The resulting pattern will be suffixed with a \f(CW\*(C`$\*(C'\fR line boundary assertion when the value is true. Set to 0 to disable. .PP .Vb 2 \& # turn it off \& $r\->anchor_line_end(0); .Ve .SS "anchor_line" .IX Subsection "anchor_line" The resulting pattern will be have the \f(CW\*(C`^\*(C'\fR and \f(CW\*(C`$\*(C'\fR line boundary assertions at the beginning and end of the pattern, respectively, when the value is true. Set to 0 to disable. .PP .Vb 3 \& $r\->add(qw(cat carrot) \& \->anchor_line \& \->as_string; # produces \*(Aq^ca(?:rro)t$\*(Aq .Ve .SS "anchor_string_begin" .IX Subsection "anchor_string_begin" The resulting pattern will be prefixed with a \f(CW\*(C`\eA\*(C'\fR string boundary assertion when the value is true. Set to 0 to disable. .PP .Vb 1 \& $r\->anchor_string_begin(1); .Ve .SS "anchor_string_end" .IX Subsection "anchor_string_end" The resulting pattern will be suffixed with a \f(CW\*(C`\eZ\*(C'\fR string boundary assertion when the value is true. Set to 0 to disable. .PP .Vb 2 \& # disable the string boundary end anchor \& $r\->anchor_string_end(0); .Ve .SS "anchor_string_end_absolute" .IX Subsection "anchor_string_end_absolute" The resulting pattern will be suffixed with a \f(CW\*(C`\ez\*(C'\fR string boundary assertion when the value is true. Set to 0 to disable. .PP .Vb 2 \& # disable the string boundary absolute end anchor \& $r\->anchor_string_end_absolute(0); .Ve .PP If you don't understand the difference between \&\f(CW\*(C`\eZ\*(C'\fR and \f(CW\*(C`\ez\*(C'\fR, the former will probably do what you want. .SS "anchor_string" .IX Subsection "anchor_string" The resulting pattern will be have the \f(CW\*(C`\eA\*(C'\fR and \f(CW\*(C`\eZ\*(C'\fR string boundary assertions at the beginning and end of the pattern, respectively, when the value is true. Set to 0 to disable. .PP .Vb 3 \& $r\->add(qw(cat carrot) \& \->anchor_string \& \->as_string; # produces \*(Aq\eAca(?:rro)t\eZ\*(Aq .Ve .SS "anchor_string_absolute" .IX Subsection "anchor_string_absolute" The resulting pattern will be have the \f(CW\*(C`\eA\*(C'\fR and \f(CW\*(C`\ez\*(C'\fR string boundary assertions at the beginning and end of the pattern, respectively, when the value is true. Set to 0 to disable. .PP .Vb 3 \& $r\->add(qw(cat carrot) \& \->anchor_string_absolute \& \->as_string; # produces \*(Aq\eAca(?:rro)t\ez\*(Aq .Ve .SS "debug(\s-1NUMBER\s0)" .IX Subsection "debug(NUMBER)" Turns debugging on or off. Statements are printed to the currently selected file handle (\s-1STDOUT\s0 by default). If you are already using this handle, you will have to arrange to select an output handle to a file of your own choosing, before call the \f(CW\*(C`add\*(C'\fR, \f(CW\*(C`as_string\*(C'\fR or \f(CW\*(C`re\*(C'\fR) functions, otherwise it will scribble all over your carefully formatted output. .IP "\(bu" 4 Off. Turns off all debugging output. .IP "\(bu" 4 1 .Sp Add. Trace the addition of patterns. .IP "\(bu" 4 2 .Sp Reduce. Trace the process of reduction and assembly. .IP "\(bu" 4 4 .Sp Lex. Trace the lexing of the input patterns into its constituent tokens. .IP "\(bu" 4 8 .Sp Time. Print to \s-1STDOUT\s0 the time taken to load all the patterns. This is nothing more than the difference between the time the object was instantiated and the time reduction was initiated. .Sp .Vb 1 \& # load=<num> .Ve .Sp Any lengthy computation performed in the client code will be reflected in this value. Another line will be printed after reduction is complete. .Sp .Vb 1 \& # reduce=<num> .Ve .Sp The above output lines will be changed to \f(CW\*(C`load\-epoch\*(C'\fR and \&\f(CW\*(C`reduce\-epoch\*(C'\fR if the internal state of the object is corrupted and the initial timestamp is lost. .Sp The code attempts to load Time::HiRes in order to report fractional seconds. If this is not successful, the elapsed time is displayed in whole seconds. .PP Values can be added (or or'ed together) to trace everything .PP .Vb 1 \& $r\->debug(7)\->add( \*(Aq\e\ed+abc\*(Aq ); .Ve .PP Calling \f(CW\*(C`debug\*(C'\fR with no arguments turns debugging off. .SS "\fIdump()\fP" .IX Subsection "dump()" Produces a synthetic view of the internal data structure. How to interpret the results is left as an exercise to the reader. .PP .Vb 1 \& print $r\->dump; .Ve .SS "chomp(0|1)" .IX Subsection "chomp(0|1)" Turns chomping on or off. .PP \&\s-1IMPORTANT:\s0 As of version 0.24, chomping is now on by default as it makes \f(CW\*(C`add_file\*(C'\fR Just Work. The only time you may run into trouble is with \f(CW\*(C`add("\e\e$/")\*(C'\fR. So don't do that, or else explicitly turn off chomping. .PP To avoid incorporating (spurious) record separators (such as \*(L"\en\*(R" on Unix) when reading from a file, \&\f(CW\*(C`add()\*(C'\fR \f(CW\*(C`chomp\*(C'\fRs its input. If you don't want this to happen, call \f(CW\*(C`chomp\*(C'\fR with a false value. .PP .Vb 2 \& $re\->chomp(0); # really want the record separators \& $re\->add(<DATA>); .Ve .SS "fold_meta_pairs(\s-1NUMBER\s0)" .IX Subsection "fold_meta_pairs(NUMBER)" Determines whether \f(CW\*(C`\es\*(C'\fR, \f(CW\*(C`\eS\*(C'\fR and \f(CW\*(C`\ew\*(C'\fR, \f(CW\*(C`\eW\*(C'\fR and \f(CW\*(C`\ed\*(C'\fR, \f(CW\*(C`\eD\*(C'\fR are folded into a \f(CW\*(C`.\*(C'\fR (dot). Folding happens by default (for reasons of backwards compatibility, even though it is wrong when the \f(CW\*(C`/s\*(C'\fR expression modifier is active). .PP Call this method with a false value to prevent this behaviour (which is only a problem when dealing with \f(CW\*(C`\en\*(C'\fR if the \f(CW\*(C`/s\*(C'\fR expression modifier is also set). .PP .Vb 2 \& $re\->add( \*(Aq\e\ew\*(Aq, \*(Aq\e\eW\*(Aq ); \& my $clone = $re\->clone; \& \& $clone\->fold_meta_pairs(0); \& print $clone\->as_string; # prints \*(Aq.\*(Aq \& print $re\->as_string; # print \*(Aq[\eW\ew]\*(Aq .Ve .SS "indent(\s-1NUMBER\s0)" .IX Subsection "indent(NUMBER)" Sets the level of indent for pretty-printing nested groups within a pattern. See the \f(CW\*(C`as_string\*(C'\fR method for more details. When called without a parameter, no indenting is performed. .PP .Vb 2 \& $re\->indent( 4 ); \& print $re\->as_string; .Ve .SS "lookahead(0|1)" .IX Subsection "lookahead(0|1)" Turns on zero-width lookahead assertions. This is usually beneficial when you expect that the pattern will usually fail. If you expect that the pattern will usually match you will probably be worse off. .SS "flags(\s-1STRING\s0)" .IX Subsection "flags(STRING)" Sets the flags that govern how the pattern behaves (for versions of Perl up to 5.9 or so, these are \f(CW\*(C`imsx\*(C'\fR). By default no flags are enabled. .SS "modifiers(\s-1STRING\s0)" .IX Subsection "modifiers(STRING)" An alias of the \f(CW\*(C`flags\*(C'\fR method, for users familiar with \&\f(CW\*(C`Regexp::List\*(C'\fR. .SS "track(0|1)" .IX Subsection "track(0|1)" Turns tracking on or off. When this attribute is enabled, additional housekeeping information is inserted into the assembled expression using \f(CW\*(C`({...}\*(C'\fR embedded code constructs. This provides the necessary information to determine which, of the original patterns added, was the one that caused the match. .PP .Vb 4 \& $re\->track( 1 ); \& if( $target =~ /$re/ ) { \& print "$target matched by ", $re\->matched, "\en"; \& } .Ve .PP Note that when this functionality is enabled, no reduction is performed and no character classes are generated. In other words, \f(CW\*(C`brag|tag\*(C'\fR is not reduced down to \f(CW\*(C`(?:br|t)ag\*(C'\fR and \f(CW\*(C`dig|dim\*(C'\fR is not reduced to \f(CW\*(C`di[gm]\*(C'\fR. .SS "unroll_plus(0|1)" .IX Subsection "unroll_plus(0|1)" Turns the unrolling of plus metacharacters on or off. When a pattern is broken up, \f(CW\*(C`a+\*(C'\fR becomes \f(CW\*(C`a\*(C'\fR, \f(CW\*(C`a*\*(C'\fR (and \&\f(CW\*(C`b+?\*(C'\fR becomes \f(CW\*(C`b\*(C'\fR, \f(CW\*(C`b*?\*(C'\fR. This may allow the freed \f(CW\*(C`a\*(C'\fR to assemble with other patterns. Not enabled by default. .SS "lex(\s-1SCALAR\s0)" .IX Subsection "lex(SCALAR)" Change the pattern used to break a string apart into tokens. You can examine the \f(CW\*(C`eg/naive\*(C'\fR script as a starting point. .SS "reduce(0|1)" .IX Subsection "reduce(0|1)" Turns pattern reduction on or off. A reduced pattern may be considerably shorter than an unreduced pattern. Consider \&\f(CW\*(C`/sl(?:ip|op|ap)/\*(C'\fR \fIversus\fR \f(CW\*(C`/sl[aio]p/\*(C'\fR. An unreduced pattern will be very similar to those produced by \&\f(CW\*(C`Regexp::Optimizer\*(C'\fR. Reduction is on by default. Turning it off speeds assembly (but assembly is pretty fast \*(-- it's the breaking up of the initial patterns in the lexing stage that can consume a non-negligible amount of time). .SS "mutable(0|1)" .IX Subsection "mutable(0|1)" This method has been marked as \s-1DEPRECATED.\s0 It will be removed in a future release. See the \f(CW\*(C`clone\*(C'\fR method for a technique to replace its functionality. .SS "\fIreset()\fP" .IX Subsection "reset()" Empties out the patterns that have been \f(CW\*(C`add\*(C'\fRed or \f(CW\*(C`insert\*(C'\fR\-ed into the object. Does not modify the state of controller attributes such as \f(CW\*(C`debug\*(C'\fR, \f(CW\*(C`lex\*(C'\fR, \f(CW\*(C`reduce\*(C'\fR and the like. .SS "Default_Lexer" .IX Subsection "Default_Lexer" \&\fBWarning:\fR the \f(CW\*(C`Default_Lexer\*(C'\fR function is a class method, not an object method. It is a fatal error to call it as an object method. .PP The \f(CW\*(C`Default_Lexer\*(C'\fR method lets you replace the default pattern used for all subsequently created \f(CW\*(C`Regexp::Assemble\*(C'\fR objects. It will not have any effect on existing objects. (It is also possible to override the lexer pattern used on a per-object basis). .PP The parameter should be an ordinary scalar, not a compiled pattern. If the pattern fails to match all parts of the string, the missing parts will be returned as single chunks. Therefore the following pattern is legal (albeit rather cork-brained): .PP .Vb 1 \& Regexp::Assemble::Default_Lexer( \*(Aq\e\ed\*(Aq ); .Ve .PP The above pattern will split up input strings digit by digit, and all non-digit characters as single chunks. .SH "DIAGNOSTICS" .IX Header "DIAGNOSTICS" .Vb 1 \& "Cannot pass a C<refname> to Default_Lexer" .Ve .PP You tried to replace the default lexer pattern with an object instead of a scalar. Solution: You probably tried to call \&\f(CW\*(C`$obj\->Default_Lexer\*(C'\fR. Call the qualified class method instead \&\f(CW\*(C`Regexp::Assemble::Default_Lexer\*(C'\fR. .PP .Vb 1 \& "filter method not passed a coderef" \& \& "pre_filter method not passed a coderef" .Ve .PP A reference to a subroutine (anonymous or otherwise) was expected. Solution: read the documentation for the \f(CW\*(C`filter\*(C'\fR method. .PP .Vb 1 \& "duplicate pattern added: /.../" .Ve .PP The \f(CW\*(C`dup_warn\*(C'\fR attribute is active, and a duplicate pattern was added (well duh!). Solution: clean your data. .PP .Vb 1 \& "cannot open [file] for input: [reason]" .Ve .PP The \f(CW\*(C`add_file\*(C'\fR method was unable to open the specified file for whatever reason. Solution: make sure the file exists and the script has the required privileges to read it. .SH "NOTES" .IX Header "NOTES" This module has been tested successfully with a range of versions of perl, from 5.005_03 to 5.9.3. Use of 5.6.0 is not recommended. .PP The expressions produced by this module can be used with the \s-1PCRE\s0 library. .PP Remember to \*(L"double up\*(R" your backslashes if the patterns are hard-coded as constants in your program. That is, you should literally \f(CW\*(C`add(\*(Aqa\e\ed+b\*(Aq)\*(C'\fR rather than \f(CW\*(C`add(\*(Aqa\ed+b\*(Aq)\*(C'\fR. It usually will work either way, but it's good practice to do so. .PP Where possible, supply the simplest tokens possible. Don't add \&\f(CW\*(C`X(?\-\ed+){2})Y\*(C'\fR when \f(CW\*(C`X\-\ed+\-\ed+Y\*(C'\fR will do. The reason is that if you also add \f(CW\*(C`X\ed+Z\*(C'\fR the resulting assembly changes dramatically: \f(CW\*(C`X(?:(?:\-\ed+){2}Y|\-\ed+Z)\*(C'\fR \fIversus\fR \&\f(CW\*(C`X\-\ed+(?:\-\ed+Y|Z)\*(C'\fR. Since R::A doesn't perform enough analysis, it won't \*(L"unroll\*(R" the \f(CW\*(C`{2}\*(C'\fR quantifier, and will fail to notice the divergence after the first \f(CW\*(C`\-d\ed+\*(C'\fR. .PP Furthermore, when the string 'X\-123000P' is matched against the first assembly, the regexp engine will have to backtrack over each alternation (the one that ends in Y \fBand\fR the one that ends in Z) before determining that there is no match. No such backtracking occurs in the second pattern: as soon as the engine encounters the \&'P' in the target string, neither of the alternations at that point (\f(CW\*(C`\-\ed+Y\*(C'\fR or \f(CW\*(C`Z\*(C'\fR) could succeed and so the match fails. .PP \&\f(CW\*(C`Regexp::Assemble\*(C'\fR does, however, know how to build character classes. Given \f(CW\*(C`a\-b\*(C'\fR, \f(CW\*(C`axb\*(C'\fR and \f(CW\*(C`a\edb\*(C'\fR, it will assemble these into \f(CW\*(C`a[\-\edx]b\*(C'\fR. When \f(CW\*(C`\-\*(C'\fR (dash) appears as a candidate for a character class it will be the first character in the class. When \&\f(CW\*(C`^\*(C'\fR (circumflex) appears as a candidate for a character class it will be the last character in the class. .PP It also knows about meta-characters than can \*(L"absorb\*(R" regular characters. For instance, given \f(CW\*(C`X\ed\*(C'\fR and \f(CW\*(C`X5\*(C'\fR, it knows that \&\f(CW5\fR can be represented by \f(CW\*(C`\ed\*(C'\fR and so the assembly is just \f(CW\*(C`X\ed\*(C'\fR. The \*(L"absorbent\*(R" meta-characters it deals with are \f(CW\*(C`.\*(C'\fR, \f(CW\*(C`\ed\*(C'\fR, \f(CW\*(C`\es\*(C'\fR and \f(CW\*(C`\eW\*(C'\fR and their complements. It will replace \f(CW\*(C`\ed\*(C'\fR/\f(CW\*(C`\eD\*(C'\fR, \&\f(CW\*(C`\es\*(C'\fR/\f(CW\*(C`\eS\*(C'\fR and \f(CW\*(C`\ew\*(C'\fR/\f(CW\*(C`\eW\*(C'\fR by \f(CW\*(C`.\*(C'\fR (dot), and it will drop \f(CW\*(C`\ed\*(C'\fR if \f(CW\*(C`\ew\*(C'\fR is also present (as will \f(CW\*(C`\eD\*(C'\fR in the presence of \f(CW\*(C`\eW\*(C'\fR). .PP \&\f(CW\*(C`Regexp::Assemble\*(C'\fR deals correctly with \f(CW\*(C`quotemeta\*(C'\fR's propensity to backslash many characters that have no need to be. Backslashes on non-metacharacters will be removed. Similarly, in character classes, a number of characters lose their magic and so no longer need to be backslashed within a character class. Two common examples are \f(CW\*(C`.\*(C'\fR (dot) and \f(CW\*(C`$\*(C'\fR. Such characters will lose their backslash. .PP At the same time, it will also process \f(CW\*(C`\eQ...\eE\*(C'\fR sequences. When such a sequence is encountered, the inner section is extracted and \&\f(CW\*(C`quotemeta\*(C'\fR is applied to the section. The resulting quoted text is then used in place of the original unquoted text, and the \f(CW\*(C`\eQ\*(C'\fR and \f(CW\*(C`\eE\*(C'\fR metacharacters are thrown away. Similar processing occurs with the \f(CW\*(C`\eU...\eE\*(C'\fR and \f(CW\*(C`\eL...\eE\*(C'\fR sequences. This may have surprising effects when using a dispatch table. In this case, you will need to know exactly what the module makes of your input. Use the \f(CW\*(C`lexstr\*(C'\fR method to find out what's going on: .PP .Vb 1 \& $pattern = join( \*(Aq\*(Aq, @{$re\->lexstr($pattern)} ); .Ve .PP If all the digits 0..9 appear in a character class, \f(CW\*(C`Regexp::Assemble\*(C'\fR will replace them by \f(CW\*(C`\ed\*(C'\fR. I'd do it for letters as well, but thinking about accented characters and other glyphs hurts my head. .PP In an alternation, the longest paths are chosen first (for example, \&\f(CW\*(C`horse|bird|dog\*(C'\fR). When two paths have the same length, the path with the most subpaths will appear first. This aims to put the \&\*(L"busiest\*(R" paths to the front of the alternation. For example, the list \f(CW\*(C`bad\*(C'\fR, \f(CW\*(C`bit\*(C'\fR, \f(CW\*(C`few\*(C'\fR, \f(CW\*(C`fig\*(C'\fR and \f(CW\*(C`fun\*(C'\fR will produce the pattern \f(CW\*(C`(?:f(?:ew|ig|un)|b(?:ad|it))\*(C'\fR. See \fIeg/tld\fR for a real-world example of how alternations are sorted. Once you have looked at that, everything should be crystal clear. .PP When tracking is in use, no reduction is performed. nor are character classes formed. The reason is that it is too difficult to determine the original pattern afterwards. Consider the two patterns \f(CW\*(C`pale\*(C'\fR and \f(CW\*(C`palm\*(C'\fR. These should be reduced to \&\f(CW\*(C`pal[em]\*(C'\fR. The final character matches one of two possibilities. To resolve whether it matched an \f(CW\*(Aqe\*(Aq\fR or \f(CW\*(Aqm\*(Aq\fR would require keeping track of the fact that the pattern finished up in a character class, which would the require a whole lot more work to figure out which character of the class matched. Without character classes it becomes much easier. Instead, \f(CW\*(C`pal(?:e|m)\*(C'\fR is produced, which lets us find out more simply where we ended up. .PP Similarly, \f(CW\*(C`dogfood\*(C'\fR and \f(CW\*(C`seafood\*(C'\fR should form \f(CW\*(C`(?:dog|sea)food\*(C'\fR. When the pattern is being assembled, the tracking decision needs to be made at the end of the grouping, but the tail of the pattern has not yet been visited. Deferring things to make this work correctly is a vast hassle. In this case, the pattern becomes merely \&\f(CW\*(C`(?:dogfood|seafood\*(C'\fR. Tracked patterns will therefore be bulkier than simple patterns. .PP There is an open bug on this issue: .PP <http://rt.perl.org/rt3/Ticket/Display.html?id=32840> .PP If this bug is ever resolved, tracking would become much easier to deal with (none of the \f(CW\*(C`match\*(C'\fR hassle would be required \- you could just match like a regular \s-1RE\s0 and it would Just Work). .SH "SEE ALSO" .IX Header "SEE ALSO" .IP "perlre" 4 .IX Item "perlre" General information about Perl's regular expressions. .IP "re" 4 .IX Item "re" Specific information about \f(CW\*(C`use re \*(Aqeval\*(Aq\*(C'\fR. .IP "Regex::PreSuf" 4 .IX Item "Regex::PreSuf" \&\f(CW\*(C`Regex::PreSuf\*(C'\fR takes a string and chops it itself into tokens of length 1. Since it can't deal with tokens of more than one character, it can't deal with meta-characters and thus no regular expressions. Which is the main reason why I wrote this module. .IP "Regexp::Optimizer" 4 .IX Item "Regexp::Optimizer" \&\f(CW\*(C`Regexp::Optimizer\*(C'\fR produces regular expressions that are similar to those produced by R::A with reductions switched off. It's biggest drawback is that it is exponentially slower than Regexp::Assemble on very large sets of patterns. .IP "Regexp::Parser" 4 .IX Item "Regexp::Parser" Fine grained analysis of regular expressions. .IP "Regexp::Trie" 4 .IX Item "Regexp::Trie" Funnily enough, this was my working name for \f(CW\*(C`Regexp::Assemble\*(C'\fR during its development. I changed the name because I thought it was too obscure. Anyway, \f(CW\*(C`Regexp::Trie\*(C'\fR does much the same as \&\f(CW\*(C`Regexp::Optimizer\*(C'\fR and \f(CW\*(C`Regexp::Assemble\*(C'\fR except that it runs much faster (according to the author). It does not recognise meta characters (that is, 'a+b' is interpreted as 'a\e+b'). .IP "Text::Trie" 4 .IX Item "Text::Trie" \&\f(CW\*(C`Text::Trie\*(C'\fR is well worth investigating. Tries can outperform very bushy (read: many alternations) patterns. .IP "Tree::Trie" 4 .IX Item "Tree::Trie" \&\f(CW\*(C`Tree::Trie\*(C'\fR is another module that builds tries. The algorithm that \&\f(CW\*(C`Regexp::Assemble\*(C'\fR uses appears to be quite similar to the algorithm described therein, except that \f(CW\*(C`R::A\*(C'\fR solves its end-marker problem without having to rewrite the leaves. .SH "See Also" .IX Header "See Also" For alternatives to this module, consider one of: .IP "o Data::Munge" 4 .IX Item "o Data::Munge" .PD 0 .IP "o OnSearch::Regex" 4 .IX Item "o OnSearch::Regex" .IP "o Regex::PreSuf" 4 .IX Item "o Regex::PreSuf" .PD .SH "LIMITATIONS" .IX Header "LIMITATIONS" Some mildly complex cases are not handled well. See examples/failure.01.pl and <https://rt.cpan.org/Public/Bug/Display.html?id=104897>. .PP See also <https://rt.cpan.org/Public/Bug/Display.html?id=106480> for a discussion of some of the issues arising with the use of a huge number of alterations. Thanx to Slaven Rezic for the details of trie 'v' non-trie operations within Perl which influence regexp handling of alternations. .PP <Regexp::Assemble> does not attempt to find common substrings. For instance, it will not collapse \f(CW\*(C`/cabababc/\*(C'\fR down to \f(CW\*(C`/c(?:ab){3}c/\*(C'\fR. If there's a module out there that performs this sort of string analysis I'd like to know about it. But keep in mind that the algorithms that do this are very expensive: quadratic or worse. .PP \&\f(CW\*(C`Regexp::Assemble\*(C'\fR does not interpret meta-character modifiers. For instance, if the following two patterns are given: \f(CW\*(C`X\ed\*(C'\fR and \f(CW\*(C`X\ed+\*(C'\fR, it will not determine that \f(CW\*(C`\ed\*(C'\fR can be matched by \f(CW\*(C`\ed+\*(C'\fR. Instead, it will produce \f(CW\*(C`X(?:\ed|\ed+)\*(C'\fR. Along a similar line of reasoning, it will not determine that \f(CW\*(C`Z\*(C'\fR and \&\f(CW\*(C`Z\ed+\*(C'\fR is equivalent to \f(CW\*(C`Z\ed*\*(C'\fR (It will produce \f(CW\*(C`Z(?:\ed+)?\*(C'\fR instead). .PP You cannot remove a pattern that has been added to an object. You'll just have to start over again. Adding a pattern is difficult enough, I'd need a solid argument to convince me to add a \f(CW\*(C`remove\*(C'\fR method. If you need to do this you should read the documentation for the \&\f(CW\*(C`clone\*(C'\fR method. .PP \&\f(CW\*(C`Regexp::Assemble\*(C'\fR does not (yet)? employ the \f(CW\*(C`(?>...)\*(C'\fR construct. .PP The module does not produce POSIX-style regular expressions. This would be quite easy to add, if there was a demand for it. .SH "BUGS" .IX Header "BUGS" Patterns that generate look-ahead assertions sometimes produce incorrect patterns in certain obscure corner cases. If you suspect that this is occurring in your pattern, disable lookaheads. .PP Tracking doesn't really work at all with 5.6.0. It works better in subsequent 5.6 releases. For maximum reliability, the use of a 5.8 release is strongly recommended. Tracking barely works with 5.005_04. Of note, using \f(CW\*(C`\ed\*(C'\fR\-style meta-characters invariably causes panics. Tracking really comes into its own in Perl 5.10. .PP If you feed \f(CW\*(C`Regexp::Assemble\*(C'\fR patterns with nested parentheses, there is a chance that the resulting pattern will be uncompilable due to mismatched parentheses (not enough closing parentheses). This is normal, so long as the default lexer pattern is used. If you want to find out which pattern among a list of 3000 patterns are to blame (speaking from experience here), the \fIeg/debugging\fR script offers a strategy for pinpointing the pattern at fault. While you may not be able to use the script directly, the general approach is easy to implement. .PP The algorithm used to assemble the regular expressions makes extensive use of mutually-recursive functions (that is, A calls B, B calls A, ...) For deeply similar expressions, it may be possible to provoke \&\*(L"Deep recursion\*(R" warnings. .PP The module has been tested extensively, and has an extensive test suite (that achieves close to 100% statement coverage), but you never know... A bug may manifest itself in two ways: creating a pattern that cannot be compiled, such as \f(CW\*(C`a\e(bc)\*(C'\fR, or a pattern that compiles correctly but that either matches things it shouldn't, or doesn't match things it should. It is assumed that Such problems will occur when the reduction algorithm encounters some sort of edge case. A temporary work-around is to disable reductions: .PP .Vb 1 \& my $pattern = $assembler\->reduce(0)\->re; .Ve .PP A discussion about implementation details and where bugs might lurk appears in the \s-1README\s0 file. If this file is not available locally, you should be able to find a copy on the Web at your nearest \s-1CPAN\s0 mirror. .PP Seriously, though, a number of people have been using this module to create expressions anywhere from 140Kb to 600Kb in size, and it seems to be working according to spec. Thus, I don't think there are any serious bugs remaining. .PP If you are feeling brave, extensive debugging traces are available to figure out where assembly goes wrong. .PP Please report all bugs at <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Regexp\-Assemble> .PP Make sure you include the output from the following two commands: .PP .Vb 2 \& perl \-MRegexp::Assemble \-le \*(Aqprint $Regexp::Assemble::VERSION\*(Aq \& perl \-V .Ve .PP There is a mailing list for the discussion of \f(CW\*(C`Regexp::Assemble\*(C'\fR. Subscription details are available at <http://listes.mongueurs.net/mailman/listinfo/regexp\-assemble>. .SH "ACKNOWLEDGEMENTS" .IX Header "ACKNOWLEDGEMENTS" This module grew out of work I did building access maps for Postfix, a modern \s-1SMTP\s0 mail transfer agent. See <http://www.postfix.org/> for more information. I used Perl to build large regular expressions for blocking dynamic/residential \s-1IP\s0 addresses to cut down on spam and viruses. Once I had the code running for this, it was easy to start adding stuff to block really blatant spam subject lines, bogus \&\s-1HELO\s0 strings, spammer mailer-ids and more... .PP I presented the work at the French Perl Workshop in 2004, and the thing most people asked was whether the underlying mechanism for assembling the REs was available as a module. At that time it was nothing more that a twisty maze of scripts, all different. The interest shown indicated that a module was called for. I'd like to thank the people who showed interest. Hey, it's going to make \fImy\fR messy scripts smaller, in any case. .PP Thomas Drugeon was a valuable sounding board for trying out early ideas. Jean Forget and Philippe Blayo looked over an early version. H.Merijn Brandt stopped over in Paris one evening, and discussed things over a few beers. .PP Nicholas Clark pointed out that while what this module does (?:c|sh)ould be done in perl's core, as per the 2004 \s-1TODO,\s0 he encouraged me to continue with the development of this module. In any event, this module allows one to gauge the difficulty of undertaking the endeavour in C. I'd rather gouge my eyes out with a blunt pencil. .PP Paul Johnson settled the question as to whether this module should live in the Regex:: namespace, or Regexp:: namespace. If you're not convinced, try running the following one-liner: .PP .Vb 1 \& perl \-le \*(Aqprint ref qr//\*(Aq .Ve .PP Philippe Bruhat found a couple of corner cases where this module could produce incorrect results. Such feedback is invaluable, and only improves the module's quality. .SH "Machine-Readable Change Log" .IX Header "Machine-Readable Change Log" The file Changes was converted into Changelog.ini by Module::Metadata::Changes. .SH "AUTHOR" .IX Header "AUTHOR" David Landgren .PP Copyright (C) 2004\-2011. All rights reserved. .PP .Vb 1 \& http://www.landgren.net/perl/ .Ve .PP If you use this module, I'd love to hear about what you're using it for. If you want to be informed of updates, send me a note. .PP Ron Savage is co-maint of the module, starting with V 0.36. .SH "Repository" .IX Header "Repository" <https://github.com/ronsavage/Regexp\-Assemble.git> .SH "TODO" .IX Header "TODO" 1. Tree equivalencies. Currently, /contend/ /content/ /resend/ /resent/ produces (?:conten[dt]|resend[dt]) but it is possible to produce (?:cont|res)en[dt] if one can spot the common tail nodes (and walk back the equivalent paths). Or be by me my => /[bm][ey]/ in the simplest case. .PP To do this requires a certain amount of restructuring of the code. Currently, the algorithm uses a two-phase approach. In the first phase, the trie is traversed and reductions are performed. In the second phase, the reduced trie is traversed and the pattern is emitted. .PP What has to occur is that the reduction and emission have to occur together. As a node is completed, it is replaced by its string representation. This then allows child nodes to be compared for equality with a simple 'eq'. Since there is only a single traversal, the overall generation time might drop, even though the context baggage required to delve through the tree will be more expensive to carry along (a hash rather than a couple of scalars). .PP Actually, a simpler approach is to take on a secret sentinel atom at the end of every pattern, which gives the reduction algorithm sufficient traction to create a perfect trie. .PP I'm rewriting the reduction code using this technique. .PP 2. Investigate how (?>foo) works. Can it be applied? .PP 5. How can a tracked pattern be serialised? (Add freeze and thaw methods). .PP 6. Store callbacks per tracked pattern. .PP 12. utf\-8... hmmmm... .PP 14. Adding qr//'ed patterns. For example, consider \f(CW$r\fR\->add ( qr/^abc/i ) \->add( qr/^abd/ ) \->add( qr/^ab e/x ); this should admit abc abC aBc aBC abd abe as matches .PP 16. Allow a fast, unsafe tracking mode, that can be used if a(?bc)? can't happen. (Possibly carp if it does appear during traversal)? .PP 17. given a\-\ed+\-\ed+\-\ed+\-\ed+\-b, produce a(?:\-\ed+){4}\-b. Something along the lines of (.{4))(\e1+) would let the regexp engine itself be brought to bear on the matter, which is a rather appealing idea. Consider .PP .Vb 1 \& while(/(?!\e+)(\eS{2,}?)(\e1+)/g) { ... $1, $2 ... } \& \& as a starting point. .Ve .PP 19. The reduction code has become unbelievably baroque. Adding code to handle (sting,singing,sing) => s(?:(?:ing)?|t)ing was far too difficult. Adding more stuff just breaks existing behaviour. And fixing the ^abcd$ ... bug broke stuff all over again. Now that the corner cases are more clearly identified, a full rewrite of the reduction code is needed. And would admit the possibility of implementing items 1 and 17. .PP 20. Handle debug unrev with a separate bit .PP 23. Japhy's http://www.perlmonks.org/index.pl?node_id=90876 list2range regexp .PP 24. Lookahead assertions contain serious bugs (as shown by assembling powersets. Need to save more context during reduction, which in turn will simplify the preparation of the lookahead classes. See also 19. .PP 26. \fI_lex()\fR swamps the overall run-time. It stems from the decision to use a single regexp to pull apart any pattern. A suite of simpler regexp to pick of parens, char classes, quantifiers and bare tokens may be faster. (This has been implemented as \fI_fastlex()\fR, but it's only marginally faster. Perhaps split\-by\- char and lex a la C? .PP 27. We don't, as yet, unroll_plus a paren e.g. (abc)+? .PP 28. We don't reroll unrolled a a* to a+ in indented or tracked output .PP 29. Use (*MARK n) in blead for tracked patterns, and use (*FAIL) for the unmatchable pattern. .SH "LICENSE" .IX Header "LICENSE" This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.