需要一个正则表达式来匹配“img”标签中的“url”

时间:2021-11-08 15:22:17

I have a string in ruby, in which I have text like give below.

我有一个ruby中的字符串,其中有如下所示的文本。

ink = "<a href=\"https://www.abc.gov/asd/asdfg/bill/bill-a\">H.R.11461</a>"
ink = "<a href=\"https://www.abc.gov/asd/asdfg/bill/bill-b\">H.R.11461</a>"
ink = "<img class='image-small' src=\"https://www.abc.gov/asd/asdfg/bill/bill-a\">"
ink = "<img class='image-large' src=\"https://www.abc.gov/asd/asdfg/bill/bill-b\" id='image'>"
ink = "<iframe class='ifram' src=\"http://www.xyzabc.com\"></iframe>"
ink = "<iframe src=\"http://www.xyzabc.com\" id='fram-1'></iframe>"

I want to match only source URL of image, means value scr attribute of img tag by regular expression.

我只想匹配图像的源URL,用正则表达式表示img标签的value scr属性。

Output should be like,

输出应该像,

https://www.abc.gov/asd/asdfg/bill/bill-a
https://www.abc.gov/asd/asdfg/bill/bill-b

Is there any way to get expected output by regex in ruby?

有什么方法可以得到regex在ruby中的预期输出吗?

1 个解决方案

#1


2  

You can use an expression like this one:
<img.*?src=\\"(.+?)\\"

您可以使用如下表达式: *?src=\“(.+?)\”\\“

You'll need to use the global modifier for the regex to get all matches on several tags. If you are testing one string at a time, it shouldn't be necessary.

您需要使用regex的全局修饰符来获取多个标记上的所有匹配。如果一次测试一个字符串,就不需要了。

Example at regex101

例子在regex101

#1


2  

You can use an expression like this one:
<img.*?src=\\"(.+?)\\"

您可以使用如下表达式: *?src=\“(.+?)\”\\“

You'll need to use the global modifier for the regex to get all matches on several tags. If you are testing one string at a time, it shouldn't be necessary.

您需要使用regex的全局修饰符来获取多个标记上的所有匹配。如果一次测试一个字符串,就不需要了。

Example at regex101

例子在regex101