Re: RFC 198 (v2) Boolean Regexes

2000-09-27 Thread Richard Proctor



HI Tom,

Welcome to England (I presume)

 This seems very complicated.  Did you look at the Ram:6 recipe on
 expressing AND, OR, and NOT  in a regex?  For example, to do
 /FOO/  /BAR/ you need not write /FOO.*BAR|BAR.*FOO/ -- and in
 fact, should not, as it doesn't work properly on some pairs!
 For example, /CAN/  /ANAL/ can't be written /CAN.*ANAL|ANAL.*CAN/
 of you expect to match "CANAL".   Overlaps bite you.  You really
 need /(?=.*CAN)(?=.*ANAL)/ instead, which permits multiple assertions.
 Please check out the recipe I'm talking about.

 --tom, from a strange place

I will start by admiting I dont have the RAM.   I was brainstorming ideas (my
day job involves a lot of brainstorming) and trying to think of new/better ways
to do things.  I am more interested in concepts than syntax.

Richard





RFC 198 (v2) Boolean Regexes

2000-09-22 Thread Perl6 RFC Librarian

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
  Last Modified: 22 Sep 2000
  Mailing List: [EMAIL PROTECTED]
  Number: 198
  Version: 2
  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 CHANGES

V2 - Changed the "Fail Pattern", enhanced the wording for many things.

=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
(...).

=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 (RFC 112
and RFC 150).  There might be some case for this as a general capability
using some flag /b = brackets? 

=head3 Substring not matching a pattern

In RFC 166 I originally proposed (?^ pattern ).  This proposal replaces that.
Though it could be used as well outside of the (?) construct.

!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 have an alt.
 
! is chosen as it has the same basic meaning outside of regexes.

!pattern is a non greedy construct that matches any string/substring that
does not match the pattern.  

=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 all the 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, though it is most 
useful in this context.  

This is sort of equivalent to (?: (.*)(??{$_ = $1; code})) ie it matches an
arbitary long substring and deliveres it to the code.  But not dependant on
how many brackets have been