This and other RFCs are available on the web at
  http://dev.perl.org/rfc/

=head1  TITLE

Boolean Regexes

=head1 VERSION

        Maintainer: Richard Proctor <[EMAIL PROTECTED]>
        Date: 6 Sep 2000
        Mailing List: [EMAIL PROTECTED]
        Version: 1
        Number: 198
        Status: Developing

=head1 ABSTRACT

This is a development of the proposal for the "not a pattern" concept in RFC
166 V1.  Looking deeper into the handling of advanced regexs, there are
potential needs for many other concepts, to allow a regex to extract
information directly from a complex file in one go, rather than a mixture
of splits and nested regexes as is typically needed today.  With these
parsing data should become easier (in some cases). 

=head1 DESCRIPTION

It would be nice (in my opinion) to be able to build more elaborate regexes
allowing data to be mined out of a sting in one go.  These ideas allow
you to apply several patterns to one substring (each must match), to
fail a match from within, to look for patterns that do not contain other
patterns, and to handle looking for cases such as (foo.*bar)|(bar.*foo) in
a more general way of saying "A substring that contains both foo and bar".

These are ideas, at present with some proposed syntax.  The ideas are more
important than the exact syntax at this stage.  This is very much work in
progress.

I have  called these boolean regexs as they bring the concepts of and (&&)
or (||) and not(!) into the realm of regexes.

Within a boolean regex (or the boolean part of a regex), several new
symbols have meanings, and some have enhanced meanings.

=head2 The Ideas

Are these part of a boolean (?&...) construct within an existing regex, or
is the advanced syntax (and meaning of &!^$) invoked by a new flag such as /B?

These can look like line noise so the use of white space with /x is used
throughout, and it might be appropriate to enforce (or assume) /x within
these.

They are all shown here using (?&...) with /x assumed.

=head3 Boolean construct

(?&...) grabs a substring, and applies one or more tests to the substring.

=head3 Substring matching multiple patterns (&&)

(?& pattern1 && pattern2 && pattern3 )

A substring is definied that matches each pattern.

For example, the first pattern may say specify a substring of at least
30 chars, the next two have a foo and a bar.

=head3 Substring matching alternative patterns (||)

(?& pattern1 || pattern2 || pattern3)

This is similar to the existing alternative syntax "|" but the
alternatives to "|" behave as /^pattern$/ rather than /pattern/ (^ and $
taken as refereing to the substring in this case - see below).

(pattern1 || pattern2 || pattern3) can be mixed in with the && case above to
build up more advanced cases. && and || operators can be nested with brackets
in normal ways.

=head3 Brackets within boolean regexes

Within a complex boolean regex there are likely to be lots and lots of brackets
to nest and control the behaviour of the regex.  Rather than having to sprinkle
the regex with (?:) line noise, it would be nicer to just use ordinary brackets
() and only support capturing of elements by using one of the (?$=) or (?%=)
constructs that have been proposed elsewhere. 

=head3 Substring not matching a pattern

In RFC 166 I originally proposed (?^ pattern ).  This proposal replaces that.

!pattern matches anything that does not match the pattern.  On its own it is
not terribly useful, but in conjuction with && and || one can do things
such as (?& <img && ! alt=) ie does it have an image not hae an alt.
 
! is chosen as it has the same basic meaning outside of regexes.

=head3 Meaning of $ and ^ inside a boolean regex

^ and $ are taken to mean the begining and end of the substring, not begining
and and of the line/string from within a boolean regex.

=head3 Greediness

Should the (?&) construct be greedy or nongreedy?  To some extent this
depends on the elements it contains.  If a matching set of patterns are
greedy then it will be greedy, if they are not greedy then it will not be. 
This might or might be sufficient.

If the situation is ambiguous (or might be) The boolean can be expresed as
(?&? ...) to force non greediness. 

=head3 Delivering a substring to some code that generates a pass/fail

(?*{code}) delivers a substring to the code, which returns with success
or failure.  The code sees the substring as $_.  This is not dependant on the
Boolean regex concept and could be used for other things.

=head3 A Failure Token

(?F) if reached as part of a pattern is always a failure.  This can be used
outside of the (?& construct.  This is not dependant on the Boolean regex
concept and could be used for other things.

There might be a case for a compilentary Success Token (?S) though I am not
sure.

=head2 Applications and examples

Matching both foo and bar in a string.

        $string =~ /(?& foo && bar)/x;

Matching a string that does not contain baz that is at least 20 chars between
foo and bar.

        $string =~ /foo (?& .{20,} && ! baz ) bar/x;

Does a html image have both an alt and a src, and what are they?

        $string =~ /<img(?& \ alt=(?$Alt=(".*?"|\S*)) &&
                            \ src=(?$Src=(".*?"|\S*)) )>/ix;


It might be possible to have a regex that simply matches valid perl6 out of
this. (though it would be large...)!

=head1 IMPLENTATION

Implementation detail is not appropriate for this stage in the devlopment
of this RFC.  If the concepts gain approval then detailed implementation
issues become relevant.

There are two aspects to regexs - compiling and executing:

Compiling of these extended forms should be relativly straight forward,
but would need some extensions to recognise the regex as being within 
(?&) state to handle the extended syntax.  

Executing - No thoughts at all at present.
 

=head1 REFERENCES

RFC 166: Additions to regexs

RFC 112: Assignment within a regex

RFC 150: Extend regex syntax to provide for return of a hash of 
         matched subpatterns 

RFC 145: Brace-matching for Perl Regular Expressions
(or at least the discussion that followed it)



Reply via email to