正则表达式表示句点、单词,然后是冒号

2024-06-01 02:30:26 发布

您现在位置:Python中文网/ 问答频道 /正文

是否有一种正则表达式、python或javascript方法来搜索句点、单词和定义,然后将其附加到字典或其他对象

例如:

. 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.

这将成为{"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" }


Tags: orthetoisbackconditionlanguageskill
3条回答

请看下面的代码:

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 the particular definition from another
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)

用于过滤单词的正则表达式,其定义为:

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

演示:https://regex101.com/r/utgrCb/1/

  • \.\s*起始点和可选空格
  • ([^:]*)“单词”是冒号之前的所有内容
  • \s*:\s*用可选空格包围的冒号
  • ([^.]*)“定义”是最后一个点之前的一切

我想使用reduce,但最终得到了这个

&13; 第13部分,;
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)
和#13;
和#13;

相关问题 更多 >