I would like to know if someone can help to get the appropriate Regex format for this example
##-AA-## Name of a Project Information: no
I am trying to re.compile(r'')
the information where it says Name of a project.
How can I get that I've tried this
re.compile(r'(\s+([a-zA-Z]+\s+)+)')
but does not work. Or how can I get rid of the strings after PROJECT.
Try this:
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()
There are 3 groups in the pattern (each one between parenthesis):
?P<group_name>
syntaxYou can then search the text with this pattern, and extract only the middle group using it's name pattern.search(text).group('project_name')
.
strip()
is applied to remove the unwanted whitespaces.
You get:
print(project_name)
>>> 'Name of a Project'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.