带有非ASCII字符的正则模式



问题摘要

我有这个正则python代码:

in

lst =[' ', 'US$170.8980xa0billion', '[2]', 'xa0(2018)']
for i in lst:
    pat = re.compile(r'([x1F-x7F]+).+(d+)')
    results=pat.search(i)
    print(results)

我正在用我的正则表达方式得到这个ouput:

out

None
<_sre.SRE_Match object; span=(0, 11), match='US$170.8980'>
None
<_sre.SRE_Match object; span=(1, 6), match='(2018'>

所需的Ouput

理想情况下,我想获得此输出:

[US$170.8980-billion-(2018)]

这对我有用:

string = 'US$170.8980xa0billion'
pat = ''.join(re.findall('([a-zA-Z0-9$.])', string))

改编

lst = [' ', 'US$170.8980xa0billion', '[2]', 'xa0(2018)']
for i in lst:
    pat = ''.join(re.findall('([a-zA-Z0-9$.s])', i))
    print(pat)

替代:

(re.findall('([^�])', i)

也许,这个表达可能与您的想法接近,

import re
lst =[' ', 'US$170.8980xa0billion', '[2]', 'xa0(2018)']
output =''
for index,item in enumerate(lst):
    item = item.strip()
    if re.match('[d+]',item) == None:
        if index == len(lst)-1:
            output +='-'
        output += re.sub(r'[^ -~]','-', item)
print(output)

不确定。

输出

US$170.8980-billion-(2018)

最新更新