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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:46:13+00:00 2026-06-17T19:46:13+00:00

I am looking for a regular expression in php to parse a string of

  • 0

I am looking for a regular expression in php to parse a string of the following pattern. The command are wrapped by double square bracket as

[[a src="" desc=""]]

where a, src and desc are the keywords (won’t be changed). src must be given but desc is optional, the value of src or desc can be wrapped by double or single quote. And src and desc could be given in any order. For example, the following patterns are all valid

[[a src="http://a.c.d" desc ="hello"]]
[[a src   ="http://a.c.d" desc= 'hello']]
[[a desc ="hello " src=  'http://a.c.d' ]]
[[a src = "http://a.c.d" ]]
[[a    src="http://a.c.d" desc ="hello"]]

any space between value and ‘a’, ‘src’, ‘desc’, ‘=’ (without quotation) should be ignored. I am going to replace this command with html tag like

SOMETHING_EXTRACT_FROM_DESC

It seems pretty tough to think of one regex to do the work. Now I have 3 regex setup to handle difference cases separately. It looks like this

$pattern = '/\[\[a[:blank:]+src[:blank:]*=[:blank:]*"(.*?)"[:blank:]+desc[:blank:]*=[:blank:]+"(.*?)"\]\]/i';
$rtn = preg_replace($pattern, '<a href="${1}">${2}</a>', $src);

$pattern = '/\[\[a[:blank:]+desc[:blank:]*=[:blank:]*"(.*?)"[:blank:]+src[:blank:]*=[:blank:]+"(.*?)"\]\]/i';
$rtn = preg_replace($pattern, '<a href="${1}">${2}</a>', $rtn);

$pattern = '/\[\[a[:blank:]+src[:blank:]*=[:blank:]+"(.*?)"\]\]/i';
$rtn = preg_replace($pattern, '<a href="${1}">${2}</a>', $rtn);

But this doesn’t work, regular expression is hard to learn 🙁

  • 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-17T19:46:15+00:00Added an answer on June 17, 2026 at 7:46 pm

    I wrote a regular expression that matches everything you requested, but allows a bit of an overhead I”ll explain at the end. But first the regex:

    Looks like this:

    \[\[a(\s+(src|desc)\s*=\s*('[^']*'|"[^"]*")){1,2}\s*\]\]
    

    I’ll brake it down so you can understand it:

    • \[\[ ... \]\] matches [[ ... ]], the beginning and ending
    • \s matches any whitespace (space and tab), \s+ expects at least one
    • (src|desc) matches either the string src or the string desc. It’s an OR operator: match src OR desc.
    • '[^']*‘ matches two single quotes and anything in between that is not a single quote
    • "[^"]*" same with double quotes
    • ('[^']*'|"[^"]*") matches one of the above two
    • (src|desc)\s*=\s*('[^']*'|"[^"]*") matches a token like src='something'
    • {1,2} matches something once or twice, appending to the above expression, metches one or two of those tokens

    And that’s pretty much it. The only problem is that it will also match this:

    [[a src="http://a.c.d" src="http://a.c.d"]]
    

    Which I think is a mismatch. If it doesn’t bother you, you’re good to go, otherwise you’ll need to change the whole concept of using a big atom with ors (i.e.: |) and take a different approach. You could use look-aheads for example. But it will get real nasty pretty fast.

    You can test it online HERE

    The regex is much more readable if I remove the backslashes and the \s stuffs. This won’t work, but I think it will help you understand it:

    [[a ( (src|desc)=('[^']*'|"[^"]*") ){1,2} ]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to run regular expression on the following string with PHP using preg_match_all
I am working with a regular expression in PHP. I have the following string:
I need some help writing a regular expression pattern in PHP. I need to
Looking for a bit of regular expression help using PHP . What is the
I am looking for a regular expression using preg_match_all in PHP 5 that would
I am looking for a regular expression in PHP that can replace the href
I was looking to split a string based on a regular expression but I
I'm looking for a regular expression pattern that can match adjacent characters in a
I'm looking for a good example of using Regular Expressions in PHP to reverse
I'm looking for a regular expression to split a field list from an SQL

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.