I have a working block of code, but something tells me it's not the most efficient.
- start with a few strings
- if it has DBA or ATTN followed by at least any 2 characters, capture DBA or ATTN to the end of line, don't look at the next string
- strip out what was just captured
What I have below seems to do that just fine.
import re
alt_name = ""
name1 = "JUST A NAME"
name2 = "UNITED STATES STORE DBA USA INC"
name3 = "ANOTHER FIELD"
regex = re.compile(r"\b(DBA\b.{2,})|\b(ATTN\b.{2,})")
if re.search(regex, name1):
match = re.search(regex, name1)
alt_name = match.group(0)
name1 = re.sub(regex, "", name1)
elif re.search(regex, name2):
match = re.search(regex, name2)
alt_name = match.group(0)
name2 = re.sub(regex, "", name2)
elif re.search(regex, name3):
match3 = re.search(regex, name3)
alt_name = match.group(0)
name3 = re.sub(regex, "", name3)
print(name1)
print(name2)
print(name3)
print(alt_name)
Is there a way to capture and strip with just 1 line instead of searching, matching and then subbing? I'm looking for efficiency and readability. Just making it short to be clever isn't what I'm going for. Maybe this is just the way to do it?