[Solved] Regex returning True and False in the file


Though I am not compleately sure what your question demands, but as far as i comphreanded it, True and False are being printed to your file, which is not the desired output?

Well that’s because re.search returns a Match Object, For example:

>>> search_string = 'piiig'
>>> output = re.search('iii', search_string)
>>> output
<_sre.SRE_Match object at 0x7fac84d359f0>

>>> if output:
...  print "Something"
...
Something

>>> print output
<_sre.SRE_Match object at 0x7fac84d359f0>

So when you’re doing:

invalid  = re.search !=(rule,Reg)
file3.write(str(invalid))

It looks for the pattern and then prints True if the pattern matches and False if pattern doesnot matches.

You’ll have to call the .group() function in re.search module to get the result:

>>> output.group() 
'iii'

So when modified accordingly, your code will become:

file3.write(str(invalid.group()))

I’d suggest padding it up with a try/catch clause or it’ll throw an error when there is no element in the group object.

0

solved Regex returning True and False in the file