Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

what data type is returned by re.search(pattern, stringOfText) in Python? An int or string?

Consider the following code below. I was told that if regExMagic() doesn't find a match for Lucy in the string of text, it returns "None" to regExMagic(). Since "None" is already a string why do we still need to wrap regExMagic() in a str() in the comparison below?

What's interesting is when I removed str() it executed only the else statement (not seen here) for each line (image below) and it didn't throw any TypeError exception that I'm trying to compare 2 different types assuming an int could have been returned to regExMagic(). But since only the else got executed then it probably didn't even bother throwing an exception?

So my main question is why do we need to include str() in this case even though "None" gets returned as I was told to regExMagic()?

Am I missing something here?

import re

def regExMagic(pattern, string):
    objectMatch = re.search(pattern, string)
    return objectMatch

def readingLinesWithRegEx():

    try:
        with open('people.txt') as file_object:
            contents = file_object.readlines()

            for line in contents:
                pattern = 'HEllo$'

                if str(regExMagic(pattern, line)) == "None":
                    print(line)
question from:https://stackoverflow.com/questions/65912996/what-data-type-is-returned-by-re-searchpattern-stringoftext-in-python-an-int

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It returns the value None (whose type is NoneType), not the string "None". So if you want to compare it with a string you have to convert it.

But it's simpler to just compare it directly without converting:

if regExMagic(pattern, line) is None:
    print(line)

You can also just test the truth value, since any other value it returns will be a non-falsey object. So just write:

if not regExMagic(pattern, line):
    print(line)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...