简体   繁体   中英

Python Regex to match a specific expression

I have the following strings:

  1. project_identifier = "CENTRAL PROPERTY DEVELOPMENTS JHB (PTY) LTD (1220681)"
  2. plan_project_name = "BLP416223852-CENTRAL PROPERTY DEVELOPMENTS JHB (PTY) LTD"
  3. acnac_Id = "BLP416223852"
  4. project_sw_id = "1232831" I am using the following regex to match plan_project_name string: pattern = rf"({acnac_Id}|{project_sw_id})(\s -\s )({project_name})" spaces around the hyphen are allowed. I obtain the project name by striping the project_identifier string from the right as follows: project_name = str((projectname.rsplit("(",1)[0]).rstrip())

then match = re.match(pattern,plan_project_name) I am failing to find the match even when using re.search method, where am I getting it wrong?please help.

Part which you are missing are parentheses in project_name. You would have to escape them (and everything that could look like regex pattern. This code should work:

import re
project_identifier = "CENTRAL PROPERTY DEVELOPMENTS JHB (PTY) LTD (1220681)"
plan_project_name = "BLP416223852-CENTRAL PROPERTY DEVELOPMENTS JHB (PTY) LTD"
acnac_Id = "BLP416223852"
project_sw_id = "1232831" 
#I am using the following regex to match plan_project_name string: 
 
project_name = re.escape(str((project_identifier.rsplit("(",1)[0]).rstrip()))
print(project_name)

pattern = rf"({acnac_Id}|{project_sw_id})(\s*-\s*)({project_name})"
print(pattern)
match = re.match(pattern,plan_project_name)
print(match)

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