私信  •  关注

Pushpesh Kumar Rajwanshi

Pushpesh Kumar Rajwanshi 最近创建的主题
Pushpesh Kumar Rajwanshi 最近回复了
6 年前
回复了 Pushpesh Kumar Rajwanshi 创建的主题 » python regex替换所有以“:”开头的单词,下一个字符是字母

您可以使用这个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}
6 年前
回复了 Pushpesh Kumar Rajwanshi 创建的主题 » python regex将括号限制为一次

用于验证移动电话号码,如 (111)-111-1111 我不认为你需要像这样一个过于复杂和不正确的正则表达式 ([(+*)]\d{3}[(+*)][a-]\d{3}[a-]\d{4}) 即使你把起锚 ^ 和端锚 $ 它将验证以下手机号码是否有效,如果正确的话,

)111(a111a1111
*111+-111-1111

Check this demo to see how it allows invalid mobile numbers

用于验证这样的移动电话号码 (111)-111-1111 ,您可以使用以下正则表达式,

^\(\d{3}\)-\d{3}-\d{4}$

Demo for correctly validating mobile numbers

如果你想让我知道 (111)-111-1111 手机号码有效。

另外,对于验证文本,应该使用 match 功能而不是 findall 后者用于从文本中提取信息,前者用于匹配文本以获得有效性。

下面是一个示例python代码,它展示了如何验证移动电话号码,

import re

arr = ['(111)-111-1111','(((((111)-111-1111',')111(a111a1111','*111+-111-1111']

for s in arr:
 if (re.match(r'^\(\d{3}\)-\d{3}-\d{4}$', s)):
  print(s, ' --> is Valid mobile number')
 else:
  print(s, ' --> is Not Valid mobile number')

印刷品,

(111)-111-1111  --> is Valid mobile number
(((((111)-111-1111  --> is Not Valid mobile number
)111(a111a1111  --> is Not Valid mobile number
*111+-111-1111  --> is Not Valid mobile number
6 年前
回复了 Pushpesh Kumar Rajwanshi 创建的主题 » python中的字符串regex

用regex做,你可以用 [ /]+

相同的python代码是,

import re
s = 'GET /phpmyadmin HTTP/1.1'
tokens = re.split('[ /]+',s)
print(tokens)

输出如下:

['GET', 'phpmyadmin', 'HTTP', '1.1']