Given a string the task it to find its first word with some rules:
- The string can have points and commas
- A word can start with a letter, a point or space
- One word can contain one apostrophe and it stills being a valid one
For example:
assert first_word("Hello world") == "Hello"
assert first_word(" a word ") == "a"
assert first_word("don't touch it") == "don't"
assert first_word("greetings, friends") == "greetings"
assert first_word("... and so on ...") == "and"
assert first_word("hi") == "hi"
assert first_word("Hello.world") == "Hello"
The code:
def first_word(text: str) -> str:
"""
returns the first word in a given text.
"""
text = re.sub("[^A-Za-z'\s.]",'',text)
words = text.split()
for word in words:
for i in range(len(word)):
if word[i].isalpha() or word[i] == "'":
if i == len(word) - 1:
if word.find('.') != -1:
return word.split('.')[0]
else:
return word
How could we improve it?

first_word(" a word ") == "a"if a word can start with a space? \$\endgroup\$