我有一个段落需要用一个特定的关键字列表来分隔。你知道吗
以下是文本(单个字符串):
"Evaluation Note: Suspected abuse by own mother. Date 3/13/2019 ID: #N/A Contact: Not Specified Name: Cecilia Valore Address: 189 West Moncler Drive Home Phone: 353 273 400 Additional Information: Please tell me when the mother arrives, we will have a meeting with her next Monday, 3/17/2019 Author: social worker"
所以我想用python根据变量名来分隔这一段。”Evaluation Note、Date、ID、Contact、Name、Address、Home Phone、Additional Information和Author是变量名。我觉得使用regex看起来不错,但是我没有很多regex的经验。你知道吗
以下是我想做的:
import re
regex = r"Evaluation Note(?:\:)? (?P<note>\D+) Date(?:\:)? (?P<date>\D+)
ID(?:\:)? (?P<id>\D+) Contact(?:\:)? (?P<contact>\D+)Name(?:\:)? (? P<name>\D+)"
test_str = "Evaluation Note: Suspected abuse by own mother. Date 3/13/2019
ID: #N/A Contact: Not Specified Name: Cecilia Valore "
matches = re.finditer(regex, test_str, re.MULTILINE)
但没有找到任何模式。你知道吗
您可以使用搜索来获取变量的位置并相应地解析文本。你可以很容易地定制它。你知道吗
这将输出:
我希望这有帮助
您可能可以动态生成正则表达式。只要参数的顺序是固定的。你知道吗
在这里我尝试一下,它确实起作用了。它所追求的实际正则表达式类似于
Some Key(?P<some_key>.*)Some Other Key(?P<some_other_key>.*)
,以此类推。你知道吗返回:
相关问题 更多 >
编程相关推荐