I am trying to find proper names and dates between parentheses in Python using regex, but for some reason nothing seems to be working for me. While I expect it is an easy answer, it is hanging me up. Can anyone offer a solution?
I am using Python 3.7. The dataset consists of the text extracted from scholarly articles. I want to extract author's name and date of publication that always comes at the end of cited sentences. I want to make this into a list that is then stored in a pandas dataframe.
A typical sentence would be,
"Correlational factors in youth violence are still rudimentarily understood and often default to SES variables (Sheffield, 1998)."
What I am trying to extract is the (Sheffield, 1998) type of data. It will always appear in that pattern. My expected output would be
[(Sheffield, 1998)]
My current code is:
import re
test_text = ['Sentence 1 (Author, 2019).',
'Sentence 2 (Another Author, 2020)',
'Sentence 3 (First Author & Second Author, 2018)',
'Sentence 4 (Author, 2019; Another Author, 2020; Fourth Author, 2017)']
test_list = []
for elem in test_text:
test_run = re.findall(r'\((\D+), (\d+)\w*\)', str(elem))
if test_run: #if something was found
test_list.append(test_run)
print(elem) #print out to see what is going on
print(str(test_run), '\n') #print out to see what is going on
print("FULL LIST OF PULL:\n", test_list)
Now when I run this, regex is pulling the first three sentence examples fine, but not the fourth sentence example.
I appreciate any advice or help. Still learning regex and python (as my code probably shows). If there is a better way to do this that does not involve regex, I am open to learning.
Thank you in advance.