用正则表达式解析

时间:2011-06-13 16:07:51

标签: regex ios regexkitlite

我有一些像

这样的文字
some text [http://abc.com/a.jpg] here will be long text 

can be multiple line breaks again [http://a.com/a.jpg] here will be other text

blah blah

我需要转变为

<div>some text</div><img src="http://abc.com/a.jpg"/><div>here will be long text 

can be multiple line breaks again</div><img src="http://a.com/a.jpg"/><div>here will be other text

blah blah</div>

要获取<img>代码,我将\[(.*?)\]替换为<img src="$1"/>,结果为

some text<img src="http://abc.com/a.jpg"/>here will be long text 

can be multiple enters again<img src="http://a.com/a.jpg"/>here will be other text

blah blah

但是,我不知道如何将文本包装在<div>

我正在使用RegexKitLite在iPhone上做所有事情

1 个答案:

答案 0 :(得分:2)

这是最简单的方法:

  1. 将所有\[(.*?)\]替换为</div><img src="$1"/><div>
  2. 前置<div>
  3. 附加</div>
  4. 这确实有一个极端情况,结果以<div></div>开头或结尾,但这可能无关紧要。

    若是,那么:

    1. 将所有\](.*?)\[替换为]<div>$1</div>[
    2. 将所有\[(.*?)\]替换为<img src="$1"/>