[英]How can I find all paths in javascript file with regex in Python?
示例 Javascript(內容):
t.appendChild(u),t}},{10:10}],16:[function(e,t,r){e(10);t.exports=function(e){var t=document.createDocumentFragment(),r=document.createElement("img");r.setAttribute("alt",e.empty),r.id="trk_recaptcha",r.setAttribute("src","/cdn-cgi/images/trace/captcha/js/re/transparent.gif?ray="+e.ray),t.appendChild(r);var n=document.createTextNode(" ");t.appendChild(n);var a=document.createElement("input");a.id="id",a.setAttribute("name","id"),a.setAttribute("type","hidden"),a.setAttribute("value",e.ray),t.appendChild(a);var i=document.createTextNode(" ");t.appendChild(i);
t.appendChild(u),t}},{10:10}],16:[function(e,t,r){e(10);t.exports=function(e){var t=document.createDocumentFragment(),r=document.createElement("img");r.setAttribute("alt",e.empty),r.id="trk_recaptcha",r.setAttribute("sdfdsfsfds",'/test/path'),t.appendChild(r);var n=document.createTextNode(" ");t.appendChild(n);var a=document.createElement("input");a.id="id",a.setAttribute("name","id"),a.setAttribute("type","hidden"),a.setAttribute("value",e.ray),t.appendChild(a);var i=document.createTextNode(" ");t.appendChild(i);
regex = ""
endpoints = re.findall(regex, content)
我想要的輸出:
> /cdn-cgi/images/trace/captcha/js/re/transparent.gif?ray=
> /test/path
我想用正則表達式找到所有以 "/ 和 '/ 開頭的字段。我嘗試了很多 url 正則表達式,但它對我不起作用。
這應該這樣做:
regex = r"""["']\/[^"']*"""
請注意,您需要修剪匹配項中的第一個字符。 這也假設路徑中沒有引號。
考慮:
import re
txt = ... #your code
pat = r"(\"|\')(\/.*?)\1"
for el in re.findall(pat, txt):
print(el[1])
每個el
將匹配以單引號或雙引號開頭的模式。 然后是最少的字符數,然后是與開頭相同的字符(相同類型的引號)。
.*
代表任意數量的任何字符,跟在?
使其非貪婪即提供最少的字符匹配。 然后\\1
指的是第一組,因此它將匹配開頭匹配的任何類型的引號。 然后通過指定el[1]
我們返回第二組匹配,即引號內匹配的任何內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.