[英]Python finding substring between certain characters using regex and replace()
假设我有一个包含大量随机内容的字符串,如下所示:
strJunk ="asdf2adsf29Value=five&lakl23ljk43asdldl"
而且我有兴趣获得位于'Value ='和'&'之间的子串,在这个例子中它将是'5'。
我可以使用如下的正则表达式:
match = re.search(r'Value=?([^&>]+)', strJunk)
>>> print match.group(0)
Value=five
>>> print match.group(1)
five
为什么match.group(0)是整个'Value = five'而group(1)只是'five'? 我有办法让'五'成为唯一的结果吗? (这个问题源于我对正则表达式的一种微弱的把握)
我也将不得不在这个字符串中进行替换,如下所示:
val1 = match.group(1)
strJunk.replace(val1, "six", 1)
产量:
'asdf2adsf29Value=six&lakl23ljk43asdldl'
考虑到我计划一遍又一遍地执行上述两个任务(在'Value ='和'&'之间找到字符串,以及替换该值),我想知道是否还有其他更有效的方法来寻找substring并在原始字符串中替换它。 我很好地坚持我所拥有的,但我只是想确保如果有更好的方法,我不会占用更多的时间。
命名组使得之后更容易获得组内容。 编译正则表达式一次,然后重用编译对象,将比为每次使用重新编译它更有效(这是重复调用re.search时会发生的情况)。 您可以使用正向lookbehind和lookahead断言来使此正则表达式适合您要执行的替换。
>>> value_regex = re.compile("(?<=Value=)(?P<value>.*?)(?=&)")
>>> match = value_regex.search(strJunk)
>>> match.group('value')
'five'
>>> value_regex.sub("six", strJunk)
'asdf2adsf29Value=six&lakl23ljk43asdldl'
我不确定你是否正在解析URL,在这种情况下,你应该肯定使用urlparse模块。
但是,鉴于这不是您的问题,在Python中使用正则表达式拆分多个字段的能力非常快,因此您应该能够按照以下方式执行所需操作:
import re
strJunk ="asdf2adsf29Value=five&lakl23ljk43asdldl"
split_result = re.split(r'[&=]', strJunk)
split_result[1] = 'six'
print "{0}={1}&{2}".format(*split_result)
希望这可以帮助!
编辑:
如果要多次拆分,可以使用re.compile()编译正则表达式。 所以你会有:
import re
rx_split_on_delimiters = re.compile(r'[&=]') # store this somewhere
strJunk ="asdf2adsf29Value=five&lakl23ljk43asdldl"
split_result = rx_split_on_delimiters.split(strJunk)
split_result[1] = 'six'
print "{0}={1}&{2}".format(*split_result)
为什么match.group(0)是整个'Value = five'而group(1)只是'five'? 我有办法让'五'成为唯一的结果吗? (这个问题源于我对正则表达式的一种微弱的把握)
我认为断言的背后可以帮助你。
>>> match = re.search(r'(?<=Value=)([^&>]+)', strJunk)
>>> match.group(0)
'five'
但是你只能在断言后面提供一个恒定长度的字符串。
>>> match = re.search(r'(?<=Value=?)([^&>]+)', strJunk)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/re.py", line 142, in search
return _compile(pattern, flags).search(string)
File "/usr/lib/python2.6/re.py", line 245, in _compile
raise error, v # invalid expression
sre_constants.error: look-behind requires fixed-width pattern
没有正则表达式,我无法做到这一点。 你这样做的方式应该比断言后面更快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.