re.search not working. I get an syntax error when I try to run this code: str = 'an example word:cat!!' match = re.search(r 'word:\w\w\w', str) if match: print 'found', match.group() else: print 'did not find' it hangs up at the ' after the \w\w\w. I think I need to figure out how these work. thanks!
string type should be within double quotations right?
AJ, not in Python: ``` >>> s1 = 'test' >>> s2 = "test" >>> type(s1) <type 'str'> >>> type(s2) <type 'str'> >>> ```
e.mccormick's post shows what your problem appears to be: the variable name str. In Python, str is a data type, and a built in function name str(). I did your code, changing str to string and match to foo and taking that lone 'r' out of the re.search() and it ran okay. I'd recommend using: re.search( "word: [[:alnum:]]{3}", string ) instead of the '\w' to avoid the LOCALE and UNICODE stuff.
Thanks, everyone!
Join our real-time social learning platform and learn together with your friends!