[英]Can the [a-zA-Z] Python regex pattern be made to match and replace non-ASCII Unicode characters?
[英]Regex pattern with non-ASCII characters
我有此正則表達式python代碼:
在
lst =[' ', 'US$170.8980\xa0billion', '[2]', '\xa0(2018)']
for i in lst:
pat = re.compile(r'([\x1F-\x7F]+).+(\d+)')
results=pat.search(i)
print(results)
我正在使用正則表達式模式獲取此輸出:
出
None
<_sre.SRE_Match object; span=(0, 11), match='US$170.8980'>
None
<_sre.SRE_Match object; span=(1, 6), match='(2018'>
理想情況下,我想獲得以下輸出:
[US$170.8980-billion-(2018)]
也許,這種表達可能與您的想法很接近,
import re
lst =[' ', 'US$170.8980\xa0billion', '[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)
這對我有用:
string = 'US$170.8980\xa0billion'
pat = ''.join(re.findall('([a-zA-Z0-9$.])', string))
改編
lst = [' ', 'US$170.8980\xa0billion', '[2]', '\xa0(2018)']
for i in lst:
pat = ''.join(re.findall('([a-zA-Z0-9$.\s])', i))
print(pat)
選擇:
(re.findall('([^�])', i)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.