I have thousands of datasets from where I am interested in extracting the year which preceded a month. For example:
In dataset 1: September 1980
In dataset 2: October, 1978
The regular expression that I wrote using https://regex101.com/:
^(?<month>)\w+(\1)\s[0-9]{4}$|(^(?<fmonth>)\w+,\s[0-9]{4}$)
It does do the job using the link. However, when I tried to use it in my python code, I was getting the below error:
File "<ipython-input-216-a995358d0957>", line 1, in <module>
runfile('C:/Users/Muntabir/nltk_data/corpora/cookbook/clean_data/text-classification_year(clean).py', wdir='C:/Users/Muntabir/nltk_data/corpora/cookbook/clean_data')
File "C:\Users\Muntabir\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Users\Muntabir\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Muntabir/nltk_data/corpora/cookbook/clean_data/text-classification_year(clean).py", line 76, in <module>
year_data = re.findall('^(?<month>)\w+(\1)\s[0-9]{4}$|(^(?<fmonth>)\w+,\s[0-9]{4}$)', tokenized_string)
File "C:\Users\Muntabir\Anaconda3\lib\re.py", line 222, in findall
return _compile(pattern, flags).findall(string)
File "C:\Users\Muntabir\Anaconda3\lib\re.py", line 301, in _compile
p = sre_compile.compile(pattern, flags)
File "C:\Users\Muntabir\Anaconda3\lib\sre_compile.py", line 562, in compile
p = sre_parse.parse(p, flags)
File "C:\Users\Muntabir\Anaconda3\lib\sre_parse.py", line 855, in parse
p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
File "C:\Users\Muntabir\Anaconda3\lib\sre_parse.py", line 416, in _parse_sub
not nested and not items))
File "C:\Users\Muntabir\Anaconda3\lib\sre_parse.py", line 691, in _parse
len(char) + 2)
error: unknown extension ?<m
I am not sure why it is causing this error. Can anyone provide me with an explanation with a possible solution? Your help would be much appreciated.
Thanks
r'\w+,?\s+[0-9]{4}(?!\d)'