hi,

please help me to solve the error for:

text1.txt:
line1 <data>
line2 <items>
line3 <match name="item1" rhs="domain.com"></match>
line4 <match name="item2" rhs="domainn.com"></match>
line5 <match name="item2" rhs="1010data.com"></match>
line6 </items>
line7 </data>

text2.txt:
line1 djshjsdf
line2 sdfngjfg

check domain.com,domain.com,1010data.com in text2.com, if not there print domain.com,domain.com,1010data.com in to the 3rd text file(text3.txt)

import re
with open('C:\\Users\\Desktop\\m\\test1.txt', 'r') as f_in:
    with open('C:\\Users\\Desktop\\m\\test_compare.txt', 'r') as f_compare:
        with open('C:\\Users\\Desktop\\m\\result.txt', 'w') as f_out:
            d1 = f_in.read()
            d2 = f_compare.read()
            for match in re.finditer(r'rhs="(.*)"', d1):
                    if match not in d2:                         
                            f_out.write('{}\n'.format(match.group(1)))

while running the above code, it throws an error

Traceback (most recent call last):
  File "C:\Python27\comparetwofiles\src\compare\notepadtest.py", line 11, in <module>
    if match not in d2:                         
TypeError: 'in <string>' requires string as left operand, not _sre.SRE_Match

can anyone help me to fix the above error.....

Dani AI

Generated

Quick diagnosis: the TypeError came from testing a regex Match object for membership in a string — membership checks against a string need a string on the left. was right to recommend extracting strings (findall) instead of working with Match objects directly, and ’s set-diff idea is the right direction. Two practical improvements follow: use a regex that stops at the next quote (avoid greedy .*), and normalize values before comparing so case and trivial formatting differences don’t produce false misses.

A concise, robust workflow: extract the rhs attribute values with a safe pattern, extract any domains found in the second file with a domain-pattern, normalize to lowercase, compute set difference, and write the missing domains one per line. Example:

import re

rhs_rx = re.compile(r'rhs="([^"]+)"')
domain_rx = re.compile(r'\b(?:[A-Za-z0-9-]+\.)+[A-Za-z]{2,}\b')

with open('text1.txt') as f1, open('text2.txt') as f2:
    src = {m.lower() for m in rhs_rx.findall(f1.read())}
    tgt = {m.lower() for m in domain_rx.findall(f2.read())}

missing = sorted(src - tgt)

with open('text3.txt', 'w') as out:
    if missing:
        out.write('\n'.join(missing) + '\n')

Notes and edge cases: if rhs may be single-quoted, allow either quote in the pattern; if text2 contains rhs attributes instead of plain domains, reuse the same rhs extractor; if exact duplicates are required (not deduped), use a list filter rather than sets or use collections.Counter to preserve counts. For very large files, stream line-by-line with finditer to avoid reading entire files into memory. Normalizing (strip protocol, www., trailing slashes) may be necessary for real-world URLs.

Summary: replace Match objects with strings (match.group(1) or findall), use a non-greedy/safe pattern, normalize and use set difference for correctness and speed — this builds on and ’s suggestions while avoiding the greedy-pattern pitfall.

Recommended Answers

All 2 Replies

I'm not sure how you're supposedto use re.finditer but not this way. The elements of the returned list are match objects, not strings. I suggest you use findall instead. If I do this:

 lst1=re.findall(r'rhs="(.*)"',d1)

I get this:

['domain.com', 'domainn.com', '1010data.com']

You first post.
http://www.daniweb.com/software-development/python/threads/470301/problem-in-copying-domain-name-from-one-notepad-to-another-using-regex
You have postet more info about the task,but it still not as clear as it should be.
You can try this.

import re

with open('1.txt') as f1,open('2.txt') as f2,open('result.txt', 'w') as f_out:
    f1 = re.findall(r'rhs="(.*)"', f1.read())
    f2 = re.findall(r'rhs="(.*)"', f2.read())
    diff = [i for i in f1 if i not in f2]
    #print diff
    f_out.write(','.join(diff))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.