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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:10:07+00:00 2026-06-07T07:10:07+00:00

I have a problem with a regex I wrote to match shortcodes in PHP.

  • 0

I have a problem with a regex I wrote to match shortcodes in PHP.

This is the pattern, where $shortcode is the name of the shortcode:

\[$shortcode(.+?)?\](?:(.+?)?\[\/$shortcode\])?

Now, this regex behaves pretty much fine with these formats:

  • [shortcode]
  • [shortcode=value]
  • [shortcode key=value]
  • [shortcode=value]Text[/shortcode]
  • [shortcode key1=value1 key2=value2]Text[shortcode]

But it seems to have problems with the most common format,

  • [shortcode]Text[/shortcode]

which returns as matches the following:

Array
(
    [0] => [shortcode]Text[/shortcode]
    [1] => ]Text[/shortcode
)

As you can see, the second match (which should be the text, as the first is optional) includes the end of the opening tag and all the closing tag but the last bracket.

EDIT: Found out that the match returned is the first capture, not the second. See the regex in Regexr.

Can you help with this please? I’m really crushing my head on this one.

  • 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-07T07:10:09+00:00Added an answer on June 7, 2026 at 7:10 am

    In your regex:

    \[$shortcode(.+?)?\](?:(.+?)?\[\/$shortcode\])?
    

    The first capture group (.+?) matches at least 1 character.

    The whole group is optional, but in this case it happens to match every thing up to the last ].

    The following regex works:

    \[$shortcode(.*?)?\](?:(.+?)?\[\/$shortcode\])?
    

    The * quantifier means 0 or more, while + means one or more.

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

Sidebar

Related Questions

I have a problem with a difficult regex. I have this expression to detect
While doing some small regex task I came upon this problem. I have a
i write a simple Rss Feed reader now i have this problem, in the
I have been trying to solve a PHP regular expression problem for awhile now
Possible Duplicate: Java - Regex problem I have list of URLs of types: http://www.example.com/pk/etc
I have a problem with regards to writing the correct regex for the examination
I have problem with my query on C, I’m using the oci8 driver. This
I need a regex to match a word that starts with #. I wrote
I need to write a regex which will match URLs that do not have
I'm matching identifiers, but now I have a problem: my identifiers are allowed to

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.