What this code does is extract a verb and the information that follows after it. Then create a .txt file with the name of the verb and write the information inside.
I have to run to win the race
import re, os
regex_patron_01 = r"\s*\¿?(?:have to|haveto|must to|mustto)\s*((?:\w\s*)+)\?"
n = re.search(regex_patron_01, input_text_to_check, re.IGNORECASE)
if n:
word, = n.groups()
try:
word = word.strip()
except AttributeError:
print("no verb specified!!!")
regex_patron_01 = r"\s*((?:\w+)?) \s*((?:\w\s*)+)\s*\??"
n = re.search(regex_patron_01, word, re.IGNORECASE)
if n:
#This will have to be repeated for all the verbs that are present in the sentence.
verb, order_to_remember = n.groups()
verb = verb.strip()
order_to_remember = order_to_remember.strip()
target_file = target_file + verb + ".txt"
with open(target_file, 'w') as f:
f.write(order_to_remember)
This make a "run.txt", and white in this file : "to win the race"
but now I need that in addition to that, the regex can be extended to the possibility that there is more than one verb, for example
I have to run, jump and hurry to win the race
In that case you should create 3 files, one with the name "run.txt", another with the name "jump.txt", and another with the name "hurry.txt", and in each of them write the line "to win the race.
The problem I'm having is how to make it repeat the process whenever a comma (,) or an "and" follows a verb.
Other example:
I have to dance and sing more to be a pop star
And make 2 files, "dance.txt" and "sing.txt", and both with the line "more to be a pop star"