Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python正则表达式:排除\r\n_Python_Regex_String - Fatal编程技术网

Python正则表达式:排除\r\n

Python正则表达式:排除\r\n,python,regex,string,Python,Regex,String,我试图从字符串中提取字母和空格,但它保留了我不希望出现在结果中的\r\n。还有一个函数可以返回我给它的正则表达式以外的结果吗 需要排除的我的代码\r\n region = ",,,Central California\r\n" #\w Matches word characters. #\s Matches whitespace print re.findall(r"[\w\s]+", region) 例外输出[“加州中部”] 输出已获得['Central California\r\n'

我试图从字符串中提取字母和空格,但它保留了我不希望出现在结果中的
\r\n
。还有一个函数可以返回我给它的正则表达式以外的结果吗


需要排除的我的代码
\r\n

region = ",,,Central California\r\n"

#\w Matches word characters.
#\s Matches whitespace
print re.findall(r"[\w\s]+", region)

例外输出<代码>[“加州中部”]

输出已获得
['Central California\r\n']


返回与正则表达式不匹配的所有内容

region = ",,,Central California\r\n"

#\W Matches nonword characters.
print re.exclude_function(r"[\W]+", region)

例外输出<代码>[“加州中部”]



\s
包含
\r
\n
,所以只需使用

re.findall(r"[\w\t ]+", region)
相反(
“\t”
是制表符,
是空格)

如果希望函数返回与正则表达式不匹配的所有内容,只需对其执行“全部替换”:

def exclude_function(regex, string):
    return re.sub(regex, "", string)

\s
包含
\r
\n
,因此只需使用

re.findall(r"[\w\t ]+", region)
相反(
“\t”
是制表符,
是空格)

如果希望函数返回与正则表达式不匹配的所有内容,只需对其执行“全部替换”:

def exclude_function(regex, string):
    return re.sub(regex, "", string)

在我看来,您正在解析csv文件。你应该考虑使用它。 要删除尾随的换行符,可以使用

如果您想捕获每个细分中的所有内容,可以做比这简单得多的事情:

re.findall(r',?([^,]+)(?:,|\r\n)', string)
# this regex captures anything between `,` and/or a newline
用字符串显示它:

>>> s = ",,,Central California\r\n"
>>> re.findall(r',?([^,]+)(?:,|\r\n)', s)
['Central California']
有多个项目:

>>> s = ",itemA,itemB,Central California\r\n"
>>> re.findall(r',?([^,]+)(?:,|\r\n)', s)
['itemA', 'itemB', 'Central California']

>>> s = "BASE,itemA,itemB,Central California\r\n"
>>> re.findall(r',?([^,]+)(?:,|\r\n)', s)
['BASE', 'itemA', 'itemB', 'Central California']

在我看来,您正在解析csv文件。你应该考虑使用它。 要删除尾随的换行符,可以使用

如果您想捕获每个细分中的所有内容,可以做比这简单得多的事情:

re.findall(r',?([^,]+)(?:,|\r\n)', string)
# this regex captures anything between `,` and/or a newline
用字符串显示它:

>>> s = ",,,Central California\r\n"
>>> re.findall(r',?([^,]+)(?:,|\r\n)', s)
['Central California']
有多个项目:

>>> s = ",itemA,itemB,Central California\r\n"
>>> re.findall(r',?([^,]+)(?:,|\r\n)', s)
['itemA', 'itemB', 'Central California']

>>> s = "BASE,itemA,itemB,Central California\r\n"
>>> re.findall(r',?([^,]+)(?:,|\r\n)', s)
['BASE', 'itemA', 'itemB', 'Central California']

谢谢,我使用一个函数来遍历一个字符串,并用“”替换我不想要的所有内容,所以这很有帮助。正则表达式确实节省了大量的编码。谢谢,我使用一个函数只需遍历一个字符串,并用“”替换我不想要的所有内容,所以这很有帮助。正则表达式确实节省了大量代码。如果您使用的是CSV文件,Python内置库比正则表达式更适合处理您的需要。如果您使用的是CSV文件,Python内置库比正则表达式更适合处理您的需要。看一看。