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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:57:14+00:00 2026-05-16T09:57:14+00:00

I have the string page-24 and want to replace 24 by any other number.

  • 0

I have the string page-24 and want to replace 24 by any other number.

The script is written in PHP and uses preg_replace, however this problem should be independent from the programming language.

My match pattern is: (.*-)(\d*)

The replace pattern would be: $1[insert number here]$2

The backreferences work, but I cannot insert a number into the given place in the replace pattern because the expression would be interpreted in a wrong way. (First backreference to 199th match, if I inserted 99 as number.)

Am I missing an escape character here?

  • 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-16T09:57:15+00:00Added an answer on May 16, 2026 at 9:57 am

    In PHP, the replacement string either looks like this:

    '${1}[insert number here]$2'
    

    Or like this (if it is in a double-quoted string):

    "\${1}[insert number here]$2"
    

    Example:

    $s = 'page-24';
    $out = preg_replace('/(.*-)(\d*)/', '${1}99$2', $s);
    echo "out is $out"; //prints 'page-9924'
    

    (You probably just want ${1}99, which would give 'page-99' rather than 'page-9924'.)

    See here for documentation in PHP. Perl uses the same notation. Javascript, as far as I can tell, doesn’t support the ${1} notation, but it will interpret $199 as ${199} if there are at least 199 matching parenthesis, as ${19}9 if there are at least 19 matching parens, and ${1}99 if there is at least 1 matching paren, or the literal string $199 if there are no matches. I don’t know about other languages’ implementations.

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

Sidebar

Related Questions

I have an html string and I want to replace any instance of an
I have a page's html stored in a string. I want to replace the
I'm pretty new to PHP and have a String which contains an HTML page.
I have this method in cs page: public String getToolTip(Object productId, Object imgBtnId) {
Am creating a script using JQuery on an ASP.NET page. Have a JSON string
I have a page with this method in CreateTicket.aspx.cs: [WebMethod()] public static string Categories()
I have a page that has 3 variables. They look like this: String[] Headers
I have a string that I want to replace the 'xx' with a line
I have some code that is used to replace certain page output with other
I have a CKEditor in my page. Like this <h:head> <title>Facelet Title</title> <script type=text/javascript

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.