首页 > 解决方案 > 为什么 python 正则表达式不匹配特殊字符?

问题描述

我想知道为什么以下不起作用。该表达式适用于 Regex101.com。但是,当我将 ’ 添加到电子表格中时,它会返回一个空数组,而不是至少匹配该字符串。

这是正则表达式:

[^A-z0-9\s,.][^-_+=]

这就是我正在寻找的:

’
Â

在这里试试(它对我有用): https ://regex101.com/

这是代码:

import pandas as pd
import chardet
import csv 
import re

def get_file_encoding(file):
    rawdata = open(file, "rb").read()
    encoding = chardet.detect(rawdata)['encoding']
    return encoding

#Type in sanitized_ACAS_FULL_1
data = 'sanitized_ACAS_FULL_1.csv'
my_encoding = get_file_encoding(data)
#print(my_encoding)
my_encoding = 'UTF-8-SIG'
df = pd.read_csv(data, encoding=my_encoding, header=None, low_memory=False)

csv_rows = df.apply(lambda x: x.tolist(), axis=1)

sanitized_rows = []
for row in csv_rows:
    for item in row:
        index = row.index(item) 
        row[index] = str(item).strip()
        if 'nan' in str(item).strip():
            row[index] = "NA"

for row in csv_rows:
    for item in row:
        sanitized_rows.append(item)

match = []
for row in sanitized_rows:
    for entry in row:   
        if re.match(r'[^A-z0-9\s,.][^-_+=]', entry):
            match.append(entry)

print(match)

标签: pythonregexpandascsv

解决方案


(\GÂ)|(\Gâ)|(\G€)|(\G™)

这将分别获得您想要的字符。如果您希望将它们分组,您可以使用(\G’)示例。记住这\G意味着比赛的开始

我希望这会有所帮助。


推荐阅读