Regex for period, word, and then colon

Viewed 90

Is there a regex, python, or javascript way to search for a period, a word, and then a definition, and after that append it to a dictionary or another object?

For example:

. Reversion: A reversion is turning back again to a previous state or condition. Rhetoric: Rhetoric is the skill or art of using language to persuade or influence people, especially language that sounds impressive but may not be sincere or honest.

This would become {"Reversion" : "A reversion is turning back again to a previous state or condition", "Rhetoric" : "Rhetoric is the skill or art of using language to persuade or influence people, especially language that sounds impressive but may not be sincere or honest" }

4 Answers

The regex to filter out the word and the definition is:

\.\s*([^:]*)\s*:\s*([^.]*)

Demo: https://regex101.com/r/utgrCb/1/

  • \.\s* the start dot and optional spaces
  • ([^:]*) the 'word' is everything before the colon
  • \s*:\s* the colon surrounded with optional spaces
  • ([^.]*) the 'definition' is everything before the final dot

Take a look at this code:

my_final_result = {}
input_str = ". Reversion: A reversion is turning back again to a previous state or condition. Rhetoric: Rhetoric is the skill or art of using language to persuade or influence people, especially language that sounds impressive but may not be sincere or honest."
# assuming that periods don't occur in definitions and only separate  definitions from each others
input_list = input_str.split(".")
for definition in input_list:
    definition_list = definition.split(":")
    if len(definition_list) == 2:  # check if definition is correct
        # save our key-value pair to dictionary. strip() deletes some possible spaces around the words
        my_final_result[definition_list[0].strip()] = definition_list[1].strip()
print(my_final_result)

I wanted to use reduce but ended up with this

const str = `Reversion: A reversion is turning back again to a previous state or condition. Rhetoric: Rhetoric is the skill or art of using language to persuade or influence people, especially language that sounds impressive but may not be sincere or honest.`

const dict = {}
const arr = str.split(/[$\.]?(\w+): /g).slice(1)
for (let i=0;i<arr.length-1;i+=2) {
  dict[arr[i]] = arr[i+1].trim()
}
console.log("dict",dict)

I inserted a few sentences in the original sentence, in this way, to show that this solution can obtain the pattern anywhere in the text.

I used reduce to transform the return from matchAll into an object.

x = `asoidn aiosjdn yaosui noapids poasm daioansoid apaoms d. Reversion: A reversion is turning back again to a previous state or condition. aasd aeoinad oianfds inauireb docn isenm opisd a. Rhetoric: Rhetoric is the skill or art of using language to persuade or influence people, especially language that sounds impressive but may not be sincere or honest.`;

matches = [...x.matchAll(/(\w+)\s*:\s*([^.:]+.)/g)];

obj = matches.reduce((dict, [_, name, text]) => (dict[name] = text, dict), {});

console.log(obj);

Related