mp252 mp252 - 4 years ago 127
Python Question

Searching regex expression, to return string with spaces

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
part so I can apply
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 =

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 (
), then one or more letters (
), then another space (
) and then finally an opening bracket making sure I have escaped it (
). Is there something wrong with my regex?

Answer Source

Once you remove the anchors (^ matches the start of string position and $ matches the end of string position), the regex will match the string. However, you may get INDONESIA with a capturing group using:


See the regex demo. will contain the value.


  • ,\s* - a comma and zero or more whitespaces (replace * with + if you want at least 1 whitespace to be present)
  • ([a-zA-Z]+) - capturing group 1 matching one or more ASCII letters
  • \s* - zero or more whitespaces
  • \( - a ( literal symbol.

Sample Python code:

import re 
text = "JAKARTA, INDONESIA (1 February 2017)"
countryRegex = re.compile(r',\s*([a-zA-Z]+)\s*\(') 
mo =
if mo:

An alternative regex that would capture anything between ,+whitespace and whitespace+( is


See this regex demo. Here, [^)]+? matches 1+ chars other than ) as few as possible.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download