简体   繁体   中英

python regex search in complex pattern

I'm new about python.

It is hard time to me.

Now I'm finding some strings in files.

I had regex but I don't know how to use it in python. It works in Eclipse.

below one is the regex.

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

this is my source I tried.

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""")

You need create a regex compiled pattern.

In fact you can directly use

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

Also use

""" 

instead of " or ' as they both are in your pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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