I am trying to search a string in python using regex for a particular word that begins with a space and ends with a space after it. The string in question that I want to search is;
JAKARTA, INDONESIA (1 February 2017)
and I want to get back the ", INDONESIA (" part so I can apply rtrim and ltrim to it. As I could also be returning United Kingdom.
I have attempted to write this code within my python code;
import re
text = "JAKARTA, INDONESIA (1 February 2017)"
countryRegex = re.compile(r'^(,)(\s)([a-zA-Z]+)(\s)(\()$')
mo = countryRegex.search(text)
print(mo.group())
However this prints out the result
AttributeError: 'NoneType' object has no attribute 'group'
Indicated to me that I am not returning any matched objects.
I then attempted to use my regex in regex 101 however it still returns an error here saying "Your regular expression does not match the subject string."
I assumed this would work as I test for literal comma (,) then a space (\s), then one or more letters ([a-zA-Z]+), then another space (\s) and then finally an opening bracket making sure I have escaped it (\(). Is there something wrong with my regex?
^and$anchors must be removed.^anchor too.^and$are used to represent the beginning and end of the string you're searching. They would only match your regex if the comma was the first character in the string, and the open parenthesis was the last.