Nathi
Nathi

Reputation: 1

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.

Upvotes: -3

Views: 112

Answers (1)

Maciej Wrobel
Maciej Wrobel

Reputation: 660

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)

Upvotes: 0

Related Questions