0

Hy, I have an issue with regex python 3.

Here is my code :

# Here is regex 1, have to find tk_common on file tmp/out-file.txt
regex1 = re.compile(r'tk_common')
with open("tmp/out-file.txt") as f:
        for line in f:
                    result = regex1.search(line)

# If found : 
if regex1.search is not None:
    print("I find tk_common in out-file")

# else :
else:
    print("I didn't find tk_common in out-file")


# And here is the second one 
regex2 = re.compile(r'tk_text')
with open("tmp/out-file.txt") as f:
        for line in f:
                    result = regex2.search(line)

if regex2.search is not None:
    print("I find tk_text in out-file")
else:
    print("I didn't fint tk_text in out-file")

My issue :

I have two print message success when i start my programm :

I find tk_common in out-file
I find tk_text in out-file

But in fact, it should not :

$ cat tmp/out-file.txt | grep "tk_common\|tk_text"
<div class="tk_common">

Any idea ? Thanks,

1
  • regex1.search and regex2.search are functions (which are not None). You are looking for result which is the result. Besides your code will only check the last line, because go to everyline and after you checked each line you investigate the result. Commented Sep 24, 2016 at 11:53

2 Answers 2

1

This line:

if regex1.search is not None:

will never be None because regex1.search refers to the search method, not to the return value of that method. Therefore your code always thinks that there is a match.

I think that you meant to check the result variable, not regex1.search.

regex1 = re.compile(r'tk_common')
with open("tmp/out-file.txt") as f:
    for line in f:
        result = regex1.search(line)
        if result is not None:
            print("I find tk_common in out-file")
            break
    else:
        print("I didn't find tk_common in out-file")

It is unnecessary to compile the re pattern because it will be cached by the re module anyway. Also, since you don't use the match object saved in result, you could just test the result of re.search() directly:

with open("tmp/out-file.txt") as f:
    for line in f:
        if re.search(r'tk_common', line) is not None:
            print("I find tk_common in out-file")
            break
    else:
        print("I didn't find tk_common in out-file")
Sign up to request clarification or add additional context in comments.

Comments

0
if regex1.search is not None:

Should be result

if result is not None

Because re.compile().search is a function, and most definitely not None. You want to look at the return value.

Also, your loop

regex1 = re.compile(r'tk_common')
with open("tmp/out-file.txt") as f:
    for line in f:
                result = regex1.search(line)

If you find it on line 1, but then not on line 2, your result is going to be None and give a false negative. You should do if result: break

Using the Regex engine is a little overkill to simple do "is this string a substring". You can just do something like this

found_tk = False
with open('filename', 'r') as file_handle:
    for line in file_handle:
        if 'tk_text' in line:
            found_tk = True
            break

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.