2

I need a function that could generate a regex. For example if I write

$pat = "My {$var} is {$var2}"

to generate a regex that when I use

preg_match($pat,"My name is Kevin",$matches);

to return

$matches[1] = 'name';
$matches[2] = 'Kevin';

and the the string could be : "My $nameword is $name and i'm $age years old"

And the problem is:

FIG1
<?php
function gen_pattern($string)
{
<--- add code for generating pattern --->
}
?>

FIG2
$pat = gen_pattern('my name is {$X}');
ereg($pat,'my name is moo',$regs);
//$regs[1] == "moo„
([^\s]*)

$pat = gen_pattern('my {$YZ} is {$IJK}');
ereg($pat,'my name is moo',$regs);
//$regs[1] == "name", $regs[2] == "moo"

My homework's description is as follows:

Figure 1 shows a function which can generate a regular expression, usable for the ereg function.

Figure 2 shows how the pattern should work. In the given string, each item {$ABC} represents a placeholder, i.e. {$X} is the single place holder in "my name is {$X}", and there are two place holders in "my {$YZ} is {$IJK}". A place holder can be of any length above zero.

In the resulting pattern, each place holder represents one single word of the text, which has to be added to the result list. I.e. {$X} represents the next word after "my name is". As a result, $regs[1] becomes "moo" if the pattern is used on text "my name is moo". Assume that we have only texts of form [a-z ]* (note the white space).

My Task:

Fill in the green spot in Figure 1. Dont bother with error handling or checking the correct syntax of given string (i.e. "my name is {{}" or similar invalid input will not occur).

And I don't know where to begin.

3
  • 3
    the ereg* family of functions is deprecated. You should not use them anymore. use the preg_* family of functions instead. Commented Jun 18, 2012 at 9:56
  • This homework is deprecated too ;) Commented Jun 18, 2012 at 9:59
  • Nice question, much better than usual homework Qs here. Correctly tagged as homework and well formatted, +1. Commented Jun 18, 2012 at 11:00

2 Answers 2

2

this may work:

function gen_pattern($str){
    $str = preg_replace('/(\{[\$a-zA-Z]+\})/', '(\w+)', $str);
    return '/'.$str.'/';
}
$p = gen_pattern('my {$xx} is {$X}');

preg_match($p, 'my name is moo', $m);

$m will be:

array(3) {
  [0] =>
  string(14) "my name is moo"
  [1] =>
  string(4) "name"
  [2] =>
  string(3) "moo"
}
Sign up to request clarification or add additional context in comments.

5 Comments

wow man, this is insane, you answered so fast ... it works, but i don't understand for the love of god that regex, it seems that is replace something ... i don't know, do you have a tutorial or link for this? thank you
@RowMinds tutorial... try to look here
and what does it means '(\w+)' , is like "retrive the matched pattern" ?
\w = "word", means [a-zA-Z] and some other chars which i don't remember. + means one or more, () means "save it" so it can be accessed by $m in preg_match
When building a regex dynamically, always make use of preg_quote(). Otherwise you face the possibility that the regex breaks, or works unintended, or in the worst case... calls eval and executes arbitrary code (the e flag, although your code isnt vulnerable here ... its getting close).
0

Something like this will help you..:

function gen_pattern($string)
{
    return "@".preg_replace("@(\{\$(.*?)\})@msi", "(?P<$2>.*?)", $string)."@";
}

so you will get a result with your variables as keys.

1 Comment

hey, please try it now :) if it will not work too, change $2 to $1 in my code. sorry, i didnt tested it, but if it will work, it will give you results with keys as your variable names..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.