Py学习  »  Python

python regex查找双引号中包含变量的字符串

Hoàng Quốc Anh • 5 年前 • 1804 次点击  

Python中使用ReGEX的代码,可以执行类似的操作

输入:

<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 也是一个变数。

谢谢。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43617
 
1804 次点击  
文章 [ 2 ]  |  最新文章 5 年前
Emma
Reply   •   1 楼
Emma    6 年前

此表达式可能适用于:

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)))
dino
Reply   •   2 楼
dino    6 年前

假设:

1)你想要的总是一个txt文件

2)字符串总是 路径 其中包含字符 /

以下模式应该有效:

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)

输出:

/用户文件/abc.txt

Link for reference