[英]How to grep only the desired position match in a single line, where there is multiple matches, using regex?
我有一個包含數百個鏈接形式的文件: https://file1.mp4" target='_blank'>HD-MQ</a> | <a href="https://file1_v2.mkv
而且,有時,該行的末尾有mp4
而不是mkv
,如下所示: https://file1.mp4" target='_blank'>HD-MQ</a> | <a href="https://file1_v2.mp4
我已經嘗試過'http.+mp4'
模式來獲取單個 url,或者在末尾使用mkv
,但它會繼續打印整行,因為 '.+' 會做到這一點,返回以http
開頭和結尾的短語帶mp4
。
如何指定正則表達式(使用 grep)只匹配一個 url,中間沒有 html 垃圾?
最終結果需要是https://file1.mp4
或https://file1_v2.mkv
,由我指定我想要的。
您可以在模式中排除雙引號:
grep -o 'https:\/\/[^"]*\.mp4' file
grep -o 'https:\/\/[^"]*\.mkv' file
或兩種類型
grep -E -o 'https:\/\/[^"]*\.(mp4|mkv)' file
您可以在 grep 中使用-o
或--only-matching
選項來僅顯示匹配的正則表達式。
那么你的正則表達式可能是這樣的:
grep -o 'https:\/\/[a-zA-Z0-9_.]*'
如果您顯示不同的文本,這不是最好的正則表達式模式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.