reducing regex to common function name

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have been given a project to complete regarding Perl regular
expressions.  I need to evaluate a given regular expression and
evaluate its effect on a string and compare to the effects of a list
of common functions and identify a match so that a Perl function may
be automatically generated with an appropriate name to encapsulate the

For example:

$out =~ s/^\s*//;
$out =~ s/\s*$//;

These two lines will trim white space from the front and back of the
string $out.  This is equivalent to the function Trim() which can be
found in many languages and function libraries.

I need to write a subroutine such that:

print conversion('s/^\s*//','s/\s*$//');

will generate the output: Trim()

of course I need to be able to match and reduce all regex's to their
equivalent common function name or return undef if there is no
conversion possible.

From there I will need to generate a perl module file containing the
Trim function which will wrap the regex.  I think I can do this part
with no problem.

I would greatly appreciate any advice as to where to start on this
project.  I admit that it is a question on our take home final exam,
so of course I do not ask for too much help, but a pointer in the
right direction would be greatly appreciated.  The professor did allow
us to ask for help on usenet with confidence that we would not be
spoon fed an answer or that at least he could check up on us.


Re: reducing regex to common function name

Quoth (Thomas Randall):
Quoted text here. Click to load it

The crucial issue here is 'how is your list of common functions
specified'? I would, for a Perl project, specify it as a mapping from
function to regex; but that makes the project trivial :).

Can you show us the format you have your list in?


           All persons, living or dead, are entirely coincidental.                                                  Kurt Vonnegut

Re: reducing regex to common function name

Ben Morrow wrote:

Quoted text here. Click to load it

Finding the regex and mapping them to functions would be less trivial
however. Especially finding combinations of regexes that can be replaced by

Sounds like jumping into the parse tree of Perl and working from there is a
good starting point.

John                               MexIT:">
                           personal page:">
        Experienced programmer available:">
            Happy Customers:">

Re: reducing regex to common function name

On Sat, 09 Oct 2004 13:37:34 -0700, Thomas Randall wrote:
Quoted text here. Click to load it

I would say that the regular expression is slightly wrong, although it
will work perfectly.

What you really want is something which removes _one or more_ whitespaces
from the front and/or the back of the string;

  $out =~ s/^\s+//;
  $out =~ s/\s+$//;

"A teacher is never a giver of truth - he is a guide, a pointer to the
 truth that each student must find for himself. A good teacher is
 merely a catalyst." (Bruce Lee)

Site Timeline