如何从 velocity 中的字符串中提取 <img ... /> 标签的 img src 值
How to extract the img src value of an <img ... /> tag from a string in velocity
我收到一个包含 img 标签的 RSS 提要,在我的后端 velocity 脚本中,我想提取 img src。
因此,RSS 提要包含一个带有 img 标签的段落,如下所示:
<p>
<img alt="" src="//path1/path2/path3/imagename.jpg?x=1499453513435" style="margin: 5px; width: 313px; height: 400px; float: left;" />
Some text goes here...
</p>
以上是RSS feed的描述元素的例子。
我正在尝试提取 img src。
我试过类似的东西:
#set($index1 = $!content.description.indexOf("<[^>]*>"))
#set($index2 = $!content.description.indexOf("/>"))
#set($index2 = $index2 + 2)
#set($imgsrc="$!content.description.substring($index1, $index2))
这是我成功提取 img src 值的图像路径(包括图像名称)的结果:
#set($index1 = $!content.description.indexOf("src="))
#set($index1 = $index1 + 5)
#set($index2 = $!content.description.indexOf("?x="))
#set($index2 = $index2)
#set($imgsrc=$!content.description.substring($index1, $index2))
我先探索了
#set($index1 = $!content.description.indexOf("src="))
#set($index1 = $index1 + 5)
#set($index2 = $!content.description.indexOf("?x="))
#set($index2 = $index2)
#set($imgsrc=$!content.description.substring($index1, $index2))
它奏效了,但考虑到图像扩展名不一定是 .jpg,我改用了替代方案
我收到一个包含 img 标签的 RSS 提要,在我的后端 velocity 脚本中,我想提取 img src。
因此,RSS 提要包含一个带有 img 标签的段落,如下所示:
<p>
<img alt="" src="//path1/path2/path3/imagename.jpg?x=1499453513435" style="margin: 5px; width: 313px; height: 400px; float: left;" />
Some text goes here...
</p>
以上是RSS feed的描述元素的例子。
我正在尝试提取 img src。
我试过类似的东西:
#set($index1 = $!content.description.indexOf("<[^>]*>"))
#set($index2 = $!content.description.indexOf("/>"))
#set($index2 = $index2 + 2)
#set($imgsrc="$!content.description.substring($index1, $index2))
这是我成功提取 img src 值的图像路径(包括图像名称)的结果:
#set($index1 = $!content.description.indexOf("src="))
#set($index1 = $index1 + 5)
#set($index2 = $!content.description.indexOf("?x="))
#set($index2 = $index2)
#set($imgsrc=$!content.description.substring($index1, $index2))
我先探索了
#set($index1 = $!content.description.indexOf("src="))
#set($index1 = $index1 + 5)
#set($index2 = $!content.description.indexOf("?x="))
#set($index2 = $index2)
#set($imgsrc=$!content.description.substring($index1, $index2))
它奏效了,但考虑到图像扩展名不一定是 .jpg,我改用了替代方案