使用正则表达式的python代码,可以执行类似的操作
输入:
<script type="text/javascript">
(function(){var d=document.domain;while (true){try{var A=window.parent.document.domain;break;}catch(e)
{};d=d.replace(/.*?(?:\.|$)/,'');if (d.length==0) break;try{document.domain=d;}catch (e){break;}}})();
window.parent.OnUploadCompleted(0,"/userfiles/abc.txt","abc.txt", "") ;</script>
带有abc.txt的变量就像self.filename
仅输出:/userfiles/abc.txt
,不包含abc.txt
问题是userfiles
也是一个变量。
谢谢。/.
答案 0 :(得分:1)
假设:
1)您想要的总是一个txt文件
2)字符串始终是 path ,其中包含字符/
以下模式应该起作用:
import re
INPUT = """<script type="text/javascript">
(function(){var d=document.domain;while (true){try{var A=window.parent.document.domain;break;}catch(e)
{};d=d.replace(/.*?(?:\.|$)/,'');if (d.length==0) break;try{document.domain=d;}catch (e){break;}}})();
window.parent.OnUploadCompleted(0,"/userfiles/abc.txt","abc.txt", "") ;</script>"""
get_path = re.search(r"\"([^\"]*\/[^\"]*txt)\"",INPUT).group(1)
print(get_path)
输出:
/userfiles/abc.txt
答案 1 :(得分:0)
此表达式可能在这里起作用:
OnUploadCompleted\([0-9]+\s*,\s*\"\/(.+?)\"
,我们所需的输出在此捕获组(.+?)
中。
Please see the demo for additional explanation.
import re
regex = r"OnUploadCompleted\([0-9]+\s*,\s*\"\/(.+?)\""
test_str = ("<script type=\"text/javascript\">\n"
"(function(){var d=document.domain;while (true){try{var A=window.parent.document.domain;break;}catch(e)\n"
" {};d=d.replace(/.*?(?:\\.|$)/,'');if (d.length==0) break;try{document.domain=d;}catch (e){break;}}})();\n"
"window.parent.OnUploadCompleted(0,\"/userfiles/abc.txt\",\"abc.txt\", \"\") ;</script>")
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))