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

  • SEARCH
  • Home
  • 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 6329691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:43:21+00:00 2026-05-24T17:43:21+00:00

I’m trying to create a string from a regular expression. I noticed that in

  • 0

I’m trying to create a string from a regular expression. I noticed that in Kohana framework’s routing system you can set a route using something similar to a regular expression. Then you can create an url matching a route you’ve set. I’m trying to do something similar, but I can’t find a clean way to do it. I’ve for example got the following regular expression:

/^(?<application>\w+)\/(?<controller>\w+)\/(?<method>\w+)\/(?<parameters>\w+)\/?$/

Now I want to be able to say that “application” equals “a”, “controller” equals “b”, “method” equals “c” and “parameters” equals “d”. Then I should get a string that replaces the parts with the values specified. I can’t find a good way to do this though. I’ve basically thought of two ways: 1) replace the corresponding values in the regular expression with the specified values, or 2) create a custom “regex syntax” that you can easily be used to create string and convert it to a “proper” regular expression when needed. Kohana uses the latter, but both ways sound quite bad to me. How would you do this?

Edit: I’ll try to clarify it a bit. I for example pass the following string to the regular expression shown above using preg_match(): “myApplication/myController/myMethod/myParameters”. This returns an array that has a couple of items, including 4 items with indexes “application”, “controller”, “method” and “parameters” with the corresponding values. Now I have to create the string “myApplication/myController/myMethod/myParameters” with the regular expression’s pattern while I only have that array with the 4 items. How can I do this using PHP?

  • 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-05-24T17:43:23+00:00Added an answer on May 24, 2026 at 5:43 pm

    It should be pretty straightforward given that preg_match has support for named capturing groups (which your regular expression is, of course, using; more here).

    An example from PHP documentation:

    <?php
    
    $str = 'foobar: 2008';
    
    preg_match('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches);
    
    /* This also works in PHP 5.2.2 (PCRE 7.0) and later, however 
     * the above form is recommended for backwards compatibility */
    // preg_match('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);
    
    print_r($matches);
    
    ?>
    

    The above example will output:

    Array
    (
        [0] => foobar: 2008
        [name] => foobar
        [1] => foobar
        [digit] => 2008
        [2] => 2008
    )
    

    So in your case, you can use your $matches array, e.g. $matches['application'].


    Edit: Okay, I did not fully understand the question.

    An obvious problem with using regular expressions to generate strings is that a regular expression can match infinite strings. For example, /cat\s+rat/ matches all of:

    cat rat
    cat  rat
    cat   rat
    

    etc.

    But in your example, nothing is undefined.

    So your inclination to define a “safe” or singly-generatable language that is a subset of regular expressions is a good one, given a narrow use case. Then you could replace occurrences of /\(\?P?<(\w+)>\)/ with the value of the capturing group in there.

    But since PHP doesn’t really let you just use the value of the capturing group to access a variable during replacement in one step, you will likely have to do this in multiple steps… e.g. matching all occurrances of the above group, extracting the named groups, and then finally doing (in a loop over $matches as $match) a simple string substitution from '(?P<' . $match . '>)' and '(?<' . $match . '>)' to the value of $$match (though in a safer way than that).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
Basically, what I'm trying to create is a page of div tags, each has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to loop through a bunch of documents I have to put

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.