Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8087645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:50:40+00:00 2026-06-05T18:50:40+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:50:42+00:00Added an answer on June 5, 2026 at 6:50 pm

    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"
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will convert a type to a string, for example:
I need to create a function that will generate 2 random numbers between x
I need a function that can return the difference between the below two dates
I need a function that returns the ASCII value of a character, including spaces,
I need a function that a user can just click a flash app button
I need a function that randomizes an array similar to what shuffle does, with
I need a function that returns the substring between two words (or two characters).
I need a function that will take a bitmap and return the bitmap with
I need a function that can parse both 1,123.12 and 1.123,12 to 1123.12 There
Here is what I'm trying to do: - i need a function that when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.