繁体   English   中英

Python - re.search 和/或 re.finditer 不匹配简单模式

[英]Python - re.search and/or re.finditer not matching simple pattern

我对从数据库中检索到的一段文本进行了以下简单搜索:

    pattern = r"%quiz%"
    lesson_pieces = []
    if re.search(pattern, lesson.lesson_text) is not None:
        lesson_piece_indices =  [m.start() for m in re.finditer(pattern,lesson.lesson_text)]

完整的代码根据文本中出现的字符串 %quiz% 将文本拆分为多个段。 然后视图显示一个片段并要求用户采取操作以查看下一个片段。 然而,当我运行这段代码时,文本并没有被分成几段,相反它似乎没有找到模式并返回整个文本字符串。 以下是一些示例文本的片段:

Visible tattoos are not permitted. You must be able to cover up the tattoo. Neck and facial tattoos are not allowed. Tattoos on hands must be covered while working.

%quiz%

If you chose to arrive in or change out...

显示时,应该显示的是:

Visible tattoos are not permitted. You must be able to cover up the tattoo. Neck and facial tattoos are not allowed. Tattoos on hands must be covered while working.

Click to Continue...

相反,上面的完整文本显示包括 %quiz% ,如果它被发现就会被提取出来。 我在这里错过了什么?

在评论中使用 kindall 的建议解决了这个问题,将代码更改为:

lesson_pieces = lesson.lesson_text.split("%quiz%")

.split正是我所需要的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM