Python使用正则表达式和replace()在某些字符之间查找子字符串 [英] Python finding substring between certain characters using regex and replace()

查看:48
本文介绍了Python使用正则表达式和replace()在某些字符之间查找子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含很多随机内容的字符串,如下所示:

strJunk ="asdf2adsf29Value=five&lakl23ljk43asdldl"

而且我对获取位于Value=' 和"&"之间的子字符串感兴趣,在本例中为5".

我可以使用如下的正则表达式:

 match = re.search(r'Value=?([^&>]+)', strJunk)>>>打印 match.group(0)值=五>>>打印 match.group(1)五

为什么 match.group(0) 是整个 'Value=five' 而 group(1) 只是 '5'?有没有办法让我得到五"作为唯一的结果?(这个问题源于我对正则表达式的了解很少)

我还必须在此字符串中进行替换,例如以下内容:

 val1 = match.group(1)strJunk.replace(val1, "六", 1)

产生的结果:

 'asdf2adsf29Value=6&lakl23ljk43asdldl'

考虑到我计划一遍又一遍地执行上述两个任务(查找 'Value=' 和 '&' 之间的字符串,以及替换该值),我想知道是否还有其他更有效的方法寻找子字符串并将其替换为原始字符串.我很好地坚持我所拥有的,但我只是想确保如果有更好的方法,我不会占用比我必须更多的时间.

解决方案

命名组使之后更容易获取组内容.编译您的正则表达式一次,然后重用已编译的对象,将比每次使用时重新编译它(这是重复调用 re.search 时会发生的情况)高效得多.您可以使用正向后视断言和前瞻断言使此正则表达式适合您想要执行的替换.

<预><代码>>>>value_regex = re.compile("(?<=Value=)(?P.*?)(?=&)")>>>匹配 = value_regex.search(strJunk)>>>match.group('值')'五'>>>value_regex.sub("六", strJunk)'asdf2adsf29Value=6&lakl23ljk43asdldl'

Suppose I have a string with lots of random stuff in it like the following:

strJunk ="asdf2adsf29Value=five&lakl23ljk43asdldl"

And I'm interested in obtaining the substring sitting between 'Value=' and '&', which in this example would be 'five'.

I can use a regex like the following:

 match = re.search(r'Value=?([^&>]+)', strJunk)
 >>> print match.group(0)
 Value=five
 >>> print match.group(1)
 five

How come match.group(0) is the whole thing 'Value=five' and group(1) is just 'five'? And is there a way for me to just get 'five' as the only result? (This question stems from me only having a tenuous grasp of regex)

I am also going to have to make a substitution in this string such such as the following:

 val1 = match.group(1)
 strJunk.replace(val1, "six", 1)    

Which yields:

 'asdf2adsf29Value=six&lakl23ljk43asdldl'

Considering that I plan on performing the above two tasks (finding the string between 'Value=' and '&', as well as replacing that value) over and over, I was wondering if there are any other more efficient ways of looking for the substring and replacing it in the original string. I'm fine sticking with what I've got but I just want to make sure that I'm not taking up more time than I have to be if better methods are out there.

解决方案

Named groups make it easier to get the group contents afterwards. Compiling your regex once, and then reusing the compiled object, will be much more efficient than recompiling it for each use (which is what happens when you call re.search repeatedly). You can use positive lookbehind and lookahead assertions to make this regex suitable for the substitution you want to do.

>>> value_regex = re.compile("(?<=Value=)(?P<value>.*?)(?=&)")
>>> match = value_regex.search(strJunk)
>>> match.group('value')
'five'
>>> value_regex.sub("six", strJunk)
'asdf2adsf29Value=six&lakl23ljk43asdldl'

这篇关于Python使用正则表达式和replace()在某些字符之间查找子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆