简体   繁体   中英

Can someone help me to get the appropriate REGEX format for the following example

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()

explanation

There are 3 groups in the pattern (each one between parenthesis):

  • The first group captures everything before the project name
  • The second group captures the project name. Note that I have given a name to that group, to retrieve it easily in the next step, using the ?P<group_name> syntax
  • The third group captures everything after the project name

You 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.

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