本文介绍了Python正则表达式支持Perl的\ G吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Perl正则表达式(显示为,但希望了解完整的内容并不是回答包含\ G元字符的问题(希望回答此问题).我想将其翻译成Python,但Python似乎不支持\ G.我该怎么办?

I have a Perl regular expression (shown here, though understanding the whole thing isn't hopefully necessary to answering this question) that contains the \G metacharacter. I'd like to translate it into Python, but Python doesn't appear to support \G. What can I do?

推荐答案

尝试以下方法:

import re
re.sub()
re.findall()
re.finditer()

例如:

# Finds all words of length 3 or 4
s = "the quick brown fox jumped over the lazy dogs."
print re.findall(r'\b\w{3,4}\b', s)

# prints ['the','fox','over','the','lazy','dogs']

这篇关于Python正则表达式支持Perl的\ G吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-17 00:04