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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:45:59+00:00 2026-06-03T16:45:59+00:00

$pattern = /\[(.*?)\]\((.*?)\)/i; $replace = <a href=\$2\ rel=\nofollow\>$1</a>; $text = blah blah [LINK1](http://example.com) blah

  • 0
$pattern = "/\[(.*?)\]\((.*?)\)/i";
$replace = "<a href=\"$2\" rel=\"nofollow\">$1</a>";
$text = "blah blah [LINK1](http://example.com) blah [LINK2](http://sub.example.com/) blah blah ?";
echo preg_replace($pattern, $replace, $text);

The above works but if a space is accidently inserted between [] and () everything breaks and the two link are mixed into one:

$text = "blah blah [LINK1] (http://example.com) blah [LINK2](http://sub.example.com/) blah blah ?";

I have a feeling it’s the laxy star that breaks it but don’t know how else to match repeating links.

  • 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-03T16:46:04+00:00Added an answer on June 3, 2026 at 4:46 pm

    If I understand you right, all you need to do really is also match any number of spaces between the two as well, for example:

    /\[([^]]*)\] *\(([^)]*)\)/i
    

    Explanation:

    \[             # Matches the opening square bracket (escaped)
    ([^]]*)        # Captures any number of characters that aren't close square brackets
    \]             # Match close square bracket (escaped)
     *             # Match any number of spaces
    \(             # Match the opening bracket (escaped)
    ([^)]*)        # Captures any number of characters that aren't close brackets
    \)             # Match the close bracket (escaped)
    

    Justification:

    I should probably justify that the reason I changed your .*? into [^]]*

    The second version is more efficient because it doesn’t need to do a huge amount of backtracking that .*? does. Additionally, once an opening [ is encountered, the .*? version will carry on looking until it finds a match, rather than failing if it is not a tag as we would want. For example, if we match the expression using .*? against:

    Sad face :[ blah [LINK1](http://sub.example.com/) blah
    

    it will match

    [ blah [LINK1]
    

    and

    http://sub.example.com/
    

    Using the [^]]* approach will mean that the input is matched correctly.

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

Sidebar

Related Questions

Im trying to make a preg-replace pattern to convert the text orderId into order-id.
I need some help to tweak this regular expression: $content = 'more <a href=http://www.test.com>test</a>
I'm attempting to replace a pattern in all my .aspx and .ascx file when
I am trying to replace one regex pattern with another regex pattern. st_srt =
I'm trying to replace a friendly url pattern with a html url notation but
There is a method Regex.Replace(string source, string pattern, string replacement) The last parameter supports
Ok so if I have this pattern: ab&bc&cd&de&ef And I need to replace all
I want to validate a regular expression pattern and replace its value with my
What I want to do is str.replace(pattern, callback) , not simply str.replace(pattern, replace_pattern) ,
How can i pass multiple patterns to the regex.replace() pattern parameter? In PHP you

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.