2016-07-25 149 views
0

我有这样的HTML代码中的字符串:拆分HTML链接标签

Hello world 
    <img src="mypicture.png" /> 
    <p>Some text in a tag</p> 
    <a href="http://www.google.fr">Link to google</a> Some Text <a href="http://www.yahoo.fr">Link to yahoo</a> End of line 
    <p>Some text in a tag</p> 
    <a attribute="some value" href="http://www.apple.com">Link to apple</a> 
    Some text 

我想这个字符串转换成数组:

0 => Hello world 
    <img src="mypicture.png" /> 
    <p>Some text in a tag</p> 
    <a href=" 

    1 => http://www.google.fr 

    2 => ">Link to google</a> Some Text <a href=" 

    3 => http://www.yahoo.fr 

    4 => ">Link to yahoo</a> End of line 
    <p>Some text in a tag</p> 
    <a attribute="some value" href=" 

    5 => http://www.apple.com 

    6 => ">Link to apple</a> 
    Some text 

我已经试过这个正则表达式。它工作正常提取的联系,但我不设法建立我的阵列...

<a (.*?)href=(.*?)\"(.+?)\"(.*?)> 
+0

尝试乐趣:'(?<= * href = \“)([^”] +)(?= \“[^>] *>) –

+0

with regex.match? – Bob5421

+0

使用'Regex.Split '。 –

回答

0

您可以链接以及之前刚刚添加的东西来捕捉任何和一切:

([\W\w]*?)(?:(<a .*?href=.*?\")(.+?)(?=\")|$)

  • 获取的每一个字符,直到...
  • 找到链接:
    • 获取ŧ他连接起来,href值(基本代码)
    • 获取字符到下一个报价
  • 文本的结束被找到(网址)

然后你只需要逐步完成每个匹配,并将pre + link添加到阵列,并将url添加到阵列中。