繁体   English   中英

复杂模式下的python正则表达式搜索

[英]python regex search in complex pattern

我是python的新手。

对我来说很难。

现在,我在文件中找到了一些字符串。

我有正则表达式,但是我不知道如何在python中使用它。 它可以在Eclipse中运行。

下面是正则表达式。

("|')\w+\1\s*=\s*?\w*?\.?suppkdid

这是我尝试的来源。

import fnmatch, re, os

regex =  ''  #Q1. How could it be possible? 

for root, dirnames, filenames in os.walk('C:\the_file_directory'):
    for filename in fnmatch.filter(filenames, '*.odi'):
        with open(os.path.join(root, filename)) as f:
            for line in f:
                                if regex.search(line) is not None: #Q2. Is it right?
                                    print '=========================='
                                    print 'filename : %s' %filename 
                                    print 'line : %s' %line
                                    print '=========================='
regex=re.compile(r"""("|')\w+\1\s*=\s*?\w*?\.?suppkdid""")

您需要创建一个正则表达式编译模式。

其实你可以直接用

if re.search(r"""("|')\w+\1\s*=\s*?\w*?\.?suppkdid""",line) is not None:

也使用

""" 

而不是"'因为它们都在您的模式中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM