-3

我有以下字符串:

  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" 我正在使用以下正则表达式来匹配 plan_project_name 字符串: pattern = rf"({acnac_Id}|{project_sw_id})(\s*-\s*)({project_name})"允许连字符周围有空格。我通过从右侧剥离 project_identifier 字符串来获取项目名称,如下所示:
project_name = str((projectname.rsplit("(",1)[0]).rstrip())

然后

match = re.match(pattern,plan_project_name)

即使使用方法,我也找不到匹配项re.search,我在哪里弄错了?请帮助。

4

1 回答 1

0

您缺少的部分是 project_name 中的括号。您将不得不转义它们(以及所有可能看起来像正则表达式模式的东西。这段代码应该可以工作:

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)
于 2022-01-25T19:17:53.763 回答