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 4119320
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:04:32+00:00 2026-05-20T23:04:32+00:00

I asked a similar question recently, but didn’t get a clear answer because I

  • 0

I asked a similar question recently, but didn’t get a clear answer because I was too specific. This one is more broad.

Does anyone know how to replace an (x) occurrence in a regex pattern?

Example: Lets say I wanted to replace the 5th occurrence of the regex pattern in a string. How would I do that?

Here is the pattern:

preg_replace('/{(.*?)\|\:(.*?)}/', 'replacement', $this->source);

@anubhava REQUESTED SAMPLE CODE (last function doesn’t work):


$sample = 'blah asada asdas  {load|:title} steve jobs {load|:css} windows apple ';


$syntax = new syntax();
$syntax->parse($sample);


class syntax {

    protected $source;
    protected $i;
    protected $r;

        // parse source
    public function parse($source) {
                // set source to protected class var
        $this->source = $source;

        // match all occurrences for regex and run loop
        $output = array();
        preg_match_all('/\{(.*?)\|\:(.*?)\}/', $this->source, $output);

                // run loop
        $i = 0;
        foreach($output[0] as $key):
            // perform run function for each occurrence, send first match before |: and second match after |:
            $this->run($output[1][$i], $output[2][$i], $i);

            $i++;
        endforeach;

        echo $this->source;

    }

        // run function
    public function run($m, $p, $i) {
                // if method is load perform actions and run inject
        switch($m):

            case 'load':
                $this->inject($i, 'content');
            break;

        endswitch;

    }

        // this function should inject the modified data, but I'm still working on this.
    private function inject($i, $r) {

          $output = preg_replace('/\{(.*?)\|\:(.*?)\}/', $r, $this->source);

    }


}


  • 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-20T23:04:33+00:00Added an answer on May 20, 2026 at 11:04 pm

    You’re misunderstanding regular expressions: they’re stateless, have no memory, and no ability to count, nothing, so you can’t know that a match is the x’th match in a string – the regex engine doesn’t have a clue. You can’t do this kind of thing with a regex for the same reason as it’s not possible to write a regex to see if a string has balanced brackets: the problem requires a memory, which, by definition, regexes do not have.

    However, a regex engine can tell you all the matches, so you’re better off using preg_match() to get a list of matches, and then modify the string using that information yourself.

    Update: is this closer to what you’re thinking of?

    <?php
    class Parser {
    
        private $i;
    
        public function parse($source) {
            $this->i = 0;
            return preg_replace_callback('/\{(.*?)\|\:(.*?)\}/', array($this, 'on_match'), $source);
        }
    
        private function on_match($m) {
            $this->i++;
    
            // Do what you processing you need on the match.
            print_r(array('m' => $m, 'i' => $this->i));
    
            // Return what you want the replacement to be.
            return $m[0] . '=>' . $this->i;
        }
    }
    
    $sample = 'blah asada asdas  {load|:title} steve jobs {load|:css} windows apple ';
    $parse = new Parser();
    $result = $parse->parse($sample);
    echo "Result is: [$result]\n";
    

    Which gives…

    Array
    (
        [m] => Array
            (
                [0] => {load|:title}
                [1] => load
                [2] => title
            )
    
        [i] => 1
    )
    Array
    (
        [m] => Array
            (
                [0] => {load|:css}
                [1] => load
                [2] => css
            )
    
        [i] => 2
    )
    Result is: [blah asada asdas  {load|:title}=>1 steve jobs {load|:css}=>2 windows apple ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked a similar question about this previously, but I did not specify that
I have asked a similar question before, but I didn't have a firm grasp
Liu Chang asked a very similar question to this one here, Linux equivalent of
Similar questions to this one have been asked but none seem to address my
A similar question has been asked: MSDN subscriptions on the cheap? , but I
I didn't see any similar questions asked on this topic, and I had to
I know this question could be similar to others but really I'm looking for
I asked similar question one month ago: Inheritance though composition? I took the examples
A similar question was already asked ( Performing a Stress Test on Web Application?
Similar questions have been asked, but nothing exactly like mine, so here goes. We

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.