Python替换正则表达式(Python replace regex)
我有一个字符串,其中有一些可能为空的属性:
[attribute1=value1, attribute2=, attribute3=value3, attribute4=]
用python,我需要用值'None'来抽空空值。 我知道我可以使用string.replace('=,','=None,').replace('=]','=None]') ,但我想知道是否有办法做它使用正则表达式,也许使用?P选项。
I have a string in which there are some attributes that may be empty:
[attribute1=value1, attribute2=, attribute3=value3, attribute4=]
With python I need to sobstitute the empty values with the value 'None'. I know I can use the string.replace('=,','=None,').replace('=]','=None]') for the string but I'm wondering if there is a way to do it using a regex, maybe with the ?P option.
原文:https://stackoverflow.com/questions/37069813
更新时间:2019-11-24 12:10
最满意答案
您可以使用
import re
s = '[attribute1=value1, attribute2=, attribute3=value3, attribute4=]'
re.sub(r'=(,|])', r'=None\1', s)
\1是括号内的匹配。
You can use
import re
s = '[attribute1=value1, attribute2=, attribute3=value3, attribute4=]'
re.sub(r'=(,|])', r'=None\1', s)
\1 is the match in parenthesis.
2016-05-06
相关问答
在Perl中,您可以执行以下操作: $string =~ s/(some_regex)/lc($1)/ge;
/e选项使替换表达式被解释为要评估的Perl代码,其返回值用作最终替换值。 lc($x)返回lc($x)的较低版本。 (不确定,但我假设lc()将在最近的Perl版本中正确处理国际字符。) /g表示全局匹配。 如果您只想要单个替换,请忽略g 。 In Perl, you can do: $string =~ s/(some_regex)/lc($1)/ge;
The /e option
...
您可以使用 import re
s = '[attribute1=value1, attribute2=, attribute3=value3, attribute4=]'
re.sub(r'=(,|])', r'=None\1', s)
\1是括号内的匹配。 You can use import re
s = '[attribute1=value1, attribute2=, attribute3=value3, attribute4=]'
re.sub(r'=(,|])', r'=Non
...
使用带有函数的re.sub作为repl参数: import re
text = "blahblahblah $%word$% blablablabla $%car$%"
words = dict(word="wassup", car="toyota")
def replacement(match):
try:
return words[match.group(1)] # Lookup replacement string
except KeyError:
...
搜索并用此正则表达式替换:搜索:替换为:“ search and replace with this regex: search for: <.> replace with: "
您可以使用re.escape来转义字符串中的所有特殊正则表达式字符,然后将转义的字符串括在[...]以便它们匹配任何字符串。 >>> re.sub("[%s]" % re.escape('!~@#$%^&*()-+={}[]:;<.>?/\''), '_', table)
'123____________|___"_______\\__,__'
但是,由于您并未真正将该正则表达式用作正则表达式,因此您可以只检查每个字符是否在该字符串中: >>>''.join("_" if c in '!~@#$
...
re模块定义了一个escape函数,它就是这样做的。 就像是: for word in words:
pattern = re.escape(word)
...
The re module defines an escape function which does just that. Something like: for word in words:
pattern = re.escape(word)
...
由于在开始时你的列类型应该是字符串,这里的所有number都应该是字符串类型 new_weather = weather2.replace({"temperature":{'-99999': np.NaN,
'-88888': np.NaN,
'[A-Za-z]':''},
"
...
>>> import re
>>> str = "<20"
>>> output = re.sub(r'
>>> output
'\r\n<20'
>>> import re
>>> str = "<20"
>>> output = re.sub(r'
>>> output
'\r\n<20'
你想要的是一个积极的外观断言 。 仅当前面有 xx匹配并替换ab 。 您可以使用以下正则表达式来匹配这样的ab : (?<=xx)ab
Perl中的工作示例 Java中的工作示例 What you want is a positive lookbehind assertion. Match and replace ab only if it is preceded by xx. You can use the following regex to match such an ab: (?<=xx
...
替换字符串中的\1和\2指的是第一个和第二个“捕获”。 捕获是图案正则表达式的一部分,括在括号中。 例如,以下是示例正则表达式中的捕获: r'\* \[(.*)\]\(#(.*)\)'
^^^^ ^^^^
所以\1指的是第一次捕获匹配的任何东西, \2指的是第二次捕获匹配的任何东西。 The \1 and \2 in the replacement string refer to the first and second "captures". Captures are p
...