2

我需要一个 python one-liner,它将返回在字符串中找到的所有 URL 并将其放入 bash 数组中。就像是:

URLs=($(echo 'foo bar baz http://blackfridaygift.info/BUu4nmkRR baz foo bar http://inhelation.com/fil/iowa/lvmk65irqibpmi972hz6xx2k.php%3FLA4i9C1606274697520fdd2ad4cf649e25ae72996c901bf1520fdd2ad4cf649e25ae72996c901bf1520fdd2ad4cf649e25ae72996c901bf1520fdd2ad4cf649e25ae72996c901bf1520fdd2ad4cf649e25ae72996c901bf1' | python -c 'something here'))

我花了最后一个小时在谷歌上搜索,但似乎找不到正确的答案。

4

1 回答 1

2

用于regular expression匹配http:....https:...

import re
import sys

matched = re.findall(r"https?:\S+", sys.stdin.read())
print(matched)

通过转换为单行...

URLS=$(echo '....' | python -c 'import re, sys; print(re.findall(r"https?:\S+", sys.stdin.read()))')
于 2020-11-25T05:23:47.970 回答