0

I understand that re.search returns a match object re.search(pattern, string, flags=0) I am testing the success of the match & then retrieving the string.

# matchTest.py --- testing match object returns

import re

# expected_pattern = suburbRegex
suburbRegex = "(?s)(,_\S+\s)"
# line leading up to and including expected_pattern
mostOfLineRex = "(?s)(^.+,\S+)"
# expected_pattern to end of line
theRestRex = "(?s),_\S+\s\w+\s(.+)"

fileLines = ['173 ANDREWS John Frances 20 Bell_Road,_Sub_urbia Semi Retired\n']

for fileLine in fileLines:
    
    result = re.search(suburbRegex, fileLine)
    # print(type(result)) # re.Match
    if(result):
        patResult = re.search(suburbRegex, fileLine).group(0)
        # print(patResult)
        # print(type(patResult)) # str
        # print(type(re.search(suburbRegex, fileLine).group(0))) # str

        start = re.search(mostOfLineRex, fileLine)
        if(start):
            start = re.search(mostOfLineRex, fileLine).group(0)
            # print(start)
            print(type(start)) # str
        end = re.search(theRestRex, fileLine)
        if(end):
            end = re.search(theRestRex, fileLine).group(0)
            # print(end)
            print(type(end)) # str
        
        newFileLine = start + ' ' + end 
    else:
        print("The listing does not have a suburb!")

# File "~\matchTest.py", line 31, in <module>
#    newFileLine = start + ' ' + end
# TypeError: can only concatenate str (not "NoneType") to str

I checked that types for start & end are <class 'str'> So why do I get NoneType error?

2
  • 1
    That line in the error is still outside the result checks. Print the checks right before that line Commented Dec 18, 2022 at 1:38
  • 1
    In your code the type check for 'start' occurs only if 'start' is 'True', that is - not 'None'. And the type check for 'end' occurs only if 'end' is True, that is - not 'None'. Commented Dec 18, 2022 at 7:33

1 Answer 1

1

You have an erroneous assumption about the return values from re.search()

>>> matches = re.search(r'[0-9]+', "testme 9"); matches; type(matches)
<re.Match object; span=(7, 8), match='9'>
<class 're.Match'>
>>> matches = re.search(r'[0-9]+', "testme"); matches; type(matches)
<class 'NoneType'>

Which explains your error when trying to concatenate start " " and end.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks all. Am new to Python. I now see NoneType as a useful error hint.

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.