簡體   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