reducing regex to common function name

T

Thomas Randall

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
regex.

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.

Tom
 
B

Ben Morrow

Quoth (e-mail address removed) (Thomas Randall):
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
regex.

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?

Ben
 
J

John Bokma

Ben said:
Quoth (e-mail address removed) (Thomas Randall):

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 :).

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

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

Tore Aursand

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

These two lines will trim white space from the front and back of the
string $out.

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+$//;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top