Question

k = 'a bunch of data and then name ""Serpin-ps""'        
print re.search(r'name\s""(\w+)""',k).group(1)

gives:

AttributeError: 'NoneType' object has no attribute 'group'

desired_output = 'Serpin-ps'

Makes sense because there is a '-' in the text.

Is there anyway to get regex to incorporate the '-' along with all the other alphanumeric characters?

Was it helpful?

Solution

You can put pre-set character classes like \w into explicit character classes. So:

print re.search(r'name\s""([-\w]+)""',k).group(1)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top