0

I'm trying to parse links using php with a structure such as [google](http://google.com), and so far I've got this:

"/\[(.+?)\]((.+?\))/is" 

The only part I can't get to work is the part with the parenthesis ')'.

Any help would be great!

[edit]

@jari - This is part of the code:

$find = array(
    "@\n@", 
    "/[**](.+?)[**]/is",
    "/\[(.+?)\]\((.+?)\)/is"
    );

$replace = array(
    "<br />",
    "<strong>$1</strong>",
    "<a href\"$1\" target=\"_blank\" rel=\"no follow\"></a>"
    );

$body = htmlspecialchars($body); 
$body = preg_replace($find, $replace, $body); 
return $body; 
Patrick
  • 480
  • 3
  • 7
  • 18
  • 1
    Specify a problem: what do you input and what do you desire to get? – sybear Feb 17 '13 at 22:00
  • You're missing one backslash on the opening `(`. But move the escaping to the outer pair rather than the inner.... `\((.+?)\)` – Michael Berkowski Feb 17 '13 at 22:00
  • This is a stricter regex: http://stackoverflow.com/questions/14599071/how-can-i-write-a-javascript-regular-expression-to-replace-hyperlinks-in-this-fo/14604030#14604030 – nhahtdh Feb 18 '13 at 10:24

2 Answers2

1

The parenthesis is a special character and usually marks sub-patterns inside your expression, so you need to escape them (just as you did with the square brackets, btw):

"/\[(.+?)\]\((.+?)\)/is"
helmbert
  • 35,797
  • 13
  • 82
  • 95
0

It should look something like:

\[([^\]]+)]\([^)]+\)

We are using [^x] which means, match anything that is not x.
We have used this to capture in group one everything after the [ which is not a ]. Same for the second group using [^)]+. Anything that is not a ).

Tom
  • 15,798
  • 4
  • 37
  • 48