I have a huge spreadsheet column filled with data like this:
Abutilon abutiloides (Jacq.) Garcke ex Hochr.
Abutilon lignosum (Cav.) G. Don
Abietinella abietina (Hedw.) Fleisch.
Hypnum L. abietinum Hedw.
Thuidium abietinum (Hedw.) Schimp.
Abronia alpina Brandegee
Abies Rogers 2009 alba Mill.
Abies amabilis (Douglas ex Loudon) Douglas ex Forbes
I want to delete all the extraneous text, leaving me with a list of scientific names, like this:
Thanks for the tip, but that didn’t work for me. It deleted just about everything, including strings starting with lower case letters. It also inserted the character & - lots of them. I wonder if there might be something wrong with my Sublime program. One of the regex’s I got from ChatGPT replaced the text with thousands of &'s.
Hmm, I am not sure what would cause &s to be inserted, but it looks like when matching regex like this you would also need to select the match case button.
import re
names = [
"Abutilon abutiloides (Jacq.) Garcke ex Hochr.",
"Abutilon lignosum (Cav.) G. Don",
"Abietinella abietina (Hedw.) Fleisch.",
"Hypnum L. abietinum Hedw.",
"Thuidium abietinum (Hedw.) Schimp.",
"Abronia alpina Brandegee",
"Abies Rogers 2009 alba Mill.",
"Abies amabilis (Douglas ex Loudon) Douglas ex Forbes"
]
pattern = r"([A-Z][a-z]+)\s([a-z]+)"
result = [re.match(pattern, name).group(0) if re.match(pattern, name)
else re.match(r"([A-Za-z]+)\s([a-z]+)", name.split()[0] + " " + next(word for word in name.split()[1:] if word.islower())).group(0)
for name in names]
# Print the result
for name in result:
print(name)
Oh my God, I never even thought about that. I thought the regex itself was designed to do that. Anyway, it took me a minute to find the case button, but I tried it, and it looks like it worked perfectly. Thanks so much for the tip.
P.S. I just found a few stings that weren’t deleted, but they appear to be very few and far between, and a couple start with foreign characters. Even with a few misses, this will still save me hours of time.
Thanks. Those might come in handy, because I’m still encountering a few quirks. I don’t have a clue about Python, but I have a little experience with JavaScript.