社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

python regex替换所有以“:”开头的单词,下一个字符是字母

user3225309 • 6 年前 • 2248 次点击  

我有一个sql select(sel=“sql select text”),其中变量定义为:var_name

select  ... from (
select  ... from ( 
select  ... from  table1
where session_started between  toDateTime(:DatumOd) and toDateTime(:DatumDo)
and session_id in (select distinct ...  from table2
    where   session_start>=toDateTime('2019-01-01 10:11:12') and session_module=:channel
            and session_start between  toDateTime(:DatumOd) and toDateTime(:DatumDo)
            and ( domain_name in (:domain) or 'All domains' in (:domain) )
            and (technology in (:technology) or 'All' in (:technology))
            and (CASE when session_principal_role='Self care' then agent_name else session_principal_role end in  (:application) 
            or 'All' in (:application) )  )
order by session_id desc , execution_id desc, step_started desc, step_id desc)
) where step_type=:step_type and ...

变量以“:”开头,以括号或空格结尾。 我必须用${var_name}替换每个:var_name。

目前我正在使用: 回复:sub(r):(\w+),r“${\1}”,sel),给出:

select  ... from (
select  ... from ( 
select  ... from  table1
where session_started between  toDateTime(${DatumOd}) and toDateTime(${DatumDo})
and session_id in (select distinct ...  from table2
    where   session_start>=toDateTime('2019-01-01 10${11}${12}') and session_module=${channel}
            and session_start between  toDateTime(${DatumOd}) and toDateTime(${DatumDo})
            and ( domain_name in (${domain}) or 'All domains' in (${domain}) )
            and (technology in (${technology}) or 'All' in (${technology}))
            and (CASE when session_principal_role='Self care' then agent_name else session_principal_role end in  (${application}) 
            or 'All' in (${application}) )  )
order by session_id desc , execution_id desc, step_started desc, step_id desc)
) where step_type=${step_type} and ...

除2019-01-01 10:11:12日期常数外,一切正常。由于其中有“:”字符,其余字符被识别为变量名。

只有在“:”字符之后,我才应该替换下一个字符。

怎么做?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/51736
文章 [ 3 ]  |  最新文章 6 年前
Wiktor Stribiżew
Reply   •   1 楼
Wiktor Stribiżew    7 年前

根据您的要求,您可以使用

s = re.sub(r'\B:([^\W\d_]\w*)', r'${\1}', s)

见 regex demo .

细节

  • \B: -一个 : 前面没有单词char(或在字符串开头)
  • ([^\W\d_]\w*) -第一组( \1 在替换模式中):
    • [^\W\d_] -任何 信
    • \w* -任何0+字母、数字、下划线。

注意:如果您只想匹配ASCII字母和数字,并且您使用的是Python 3.x,请使用 re.A 或 re.ASCII 标志:

s = re.sub(r'\B:([^\W\d_]\w*)', r'${\1}', s, flags=re.A)

Python demo :

import re
s = "select  ... from (\r\nselect  ... from ( \r\nselect  ... from  table1\r\nwhere session_started between  toDateTime(:DatumOd) and toDateTime(:DatumDo)\r\nand session_id in (select distinct ...  from table2\r\n    where   session_start>=toDateTime('2019-01-01 10:11:12') and session_module=:channel\r\n            and session_start between  toDateTime(:DatumOd) and toDateTime(:DatumDo)\r\n            and ( domain_name in (:domain) or 'All domains' in (:domain) )\r\n            and (technology in (:technology) or 'All' in (:technology))\r\n            and (CASE when session_principal_role='Self care' then agent_name else session_principal_role end in  (:application) \r\n            or 'All' in (:application) )  )\r\norder by session_id desc , execution_id desc, step_started desc, step_id desc)\r\n) where step_type=:step_type and ..."
s = re.sub(r'\B:([^\W\d_]\w*)', r'${\1}', s, flags=re.A)
print(s)

输出:

select  ... from (
select  ... from ( 
select  ... from  table1
where session_started between  toDateTime(${DatumOd}) and toDateTime(${DatumDo})
and session_id in (select distinct ...  from table2
    where   session_start>=toDateTime('2019-01-01 10:11:12') and session_module=${channel}
            and session_start between  toDateTime(${DatumOd}) and toDateTime(${DatumDo})
            and ( domain_name in (${domain}) or 'All domains' in (${domain}) )
            and (technology in (${technology}) or 'All' in (${technology}))
            and (CASE when session_principal_role='Self care' then agent_name else session_principal_role end in  (${application}) 
            or 'All' in (${application}) )  )
order by session_id desc , execution_id desc, step_started desc, step_id desc)
) where step_type=${step_type} and ...
Pushpesh Kumar Rajwanshi
Reply   •   2 楼
Pushpesh Kumar Rajwanshi    7 年前

您可以使用这个regex,它使用一个正向的前瞻性来确保它只选择后跟空格或 )

:(\w+)(?=[ )\n]|$)

Demo

看看这个Python代码,

import re

s = '''select  ... from (
select  ... from ( 
select  ... from  table1
where session_started between  toDateTime(:DatumOd) and toDateTime(:DatumDo)
and session_id in (select distinct ...  from table2
    where   session_start>=toDateTime('2019-01-01 10:11:12') and session_module=:channel
            and session_start between  toDateTime(:DatumOd) and toDateTime(:DatumDo)
            and ( domain_name in (:domain) or 'All domains' in (:domain) )
            and (technology in (:technology) or 'All' in (:technology))
            and (CASE when session_principal_role='Self care' then agent_name else session_principal_role end in  (:application) 
            or 'All' in (:application) )  )
order by session_id desc , execution_id desc, step_started desc, step_id desc)
) where step_type=:step_type and ...:DatumOd
:DatumOd'''

print(re.sub(r':(\w+)(?=[ )\n]|$)', r'${\1}',s))

只打印您想要的变量忽略日期中的冒号,

select  ... from (
select  ... from (
select  ... from  table1
where session_started between  toDateTime(${DatumOd}) and toDateTime(${DatumDo})
and session_id in (select distinct ...  from table2
    where   session_start>=toDateTime('2019-01-01 10:11:12') and session_module=${channel}
            and session_start between  toDateTime(${DatumOd}) and toDateTime(${DatumDo})
            and ( domain_name in (${domain}) or 'All domains' in (${domain}) )
            and (technology in (${technology}) or 'All' in (${technology}))
            and (CASE when session_principal_role='Self care' then agent_name else session_principal_role end in  (${application})
            or 'All' in (${application}) )  )
order by session_id desc , execution_id desc, step_started desc, step_id desc)
) where step_type=${step_type} and ...${DatumOd}
${DatumOd}
Serge Ballesta
Reply   •   3 楼
Serge Ballesta    7 年前

您可以尝试以下模式: '\W:(\w+)' 为了选择 某物 仅当冒号不在 单词 性格。它适用于那个例子,但我不确定它是否足以满足一般需求。