[英]Can someone help me to get the appropriate REGEX format for the following example
我想知道是否有人可以幫助為此示例獲取適當的正則表達式格式
##-AA-## Name of a Project Information: no
我正在嘗試re.compile(r'')
顯示項目名稱的信息。
我怎么知道我已經試過了
re.compile(r'(\s+([a-zA-Z]+\s+)+)')
但不起作用。 或者我怎樣才能擺脫 PROJECT 之后的字符串。
嘗試這個:
import re
text = "##-AA-## Name of a Project Information: no"
pattern = re.compile(r'(##-AA-##)(?P<project_name>.*?)(Information:)')
p_name = pattern.search(text).group('project_name').strip()
模式中有 3 組(括號中的每組):
?P<group_name>
語法然后,您可以使用此模式搜索文本,並使用其名稱pattern.search(text).group('project_name')
僅提取中間組。
strip()
用於刪除不需要的空格。
你得到:
print(project_name)
>>> 'Name of a Project'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.