开发者

RegEx to extract URL, target and link from custom markup

I'm sure this is really simple, but I just can't figure it out.

I want to extract the URL, target and link from this

Example:
[http://www.testdomain.c开发者_如何学Goom/blog/122_Blog Post.aspx _blank]Click here![/]

I have tried this:

\[(.*) (.*)\](.+?)\[\/\]

It works as long as I have _blank or _self following the URL, but if I remove it all together, I get Post.aspx as target (of course).

I also tried things like (_blank)?, but that didn't work at all.

Please help, RegEx really isn't my thing, and feel free to suggest other improvements. It doesn't have to be foolproof, I just want to keep it simple.

Thanks!


\[(.*?) ?(_[\w\d]+)?\](.+?)\[\/\]

the first (.*) in your regex is greedy and need to be reluctant (.*?)

with regex there are greedy, reluctant and possessive qualifier. greedy is most common but some special cases need reluctant or possessive qualifiers.

check this link to test the regex http://www.rubular.com/r/lszC0EWaWM


Try this:

\[(.*?)(?: (_blank|_self))?\](.+?)\[\/\]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜