English 中文(简体)
匹配对象和特定正则与 Python 匹配
原标题:Matching an object and a specific regex with Python

如果有文字,我需要检查每个字典是否具有 exactly(经编辑) 3个大写字母,如果有的话,将其添加到被重编的字符串中。

I wrote the following: m = re.match("[A-Z]{3}.[A-Z]{3}", text) (let s say text="AAAbAAAcAAA")

我本想在匹配对象中找到两个组:AAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

现在,当我援引 m.group(0) I get "AABAAA" 是正确的。然而,当引用 m.group(1) 时,我发现没有这样的组,意思是“AACAAA”不是匹配的。为什么?

而且,在援引 m. groups () () ) 时,我得到一个空的图普,尽管我应该得到一个匹配图普,这意味着,在我的情况下,我应该得到一个“AAABAAA”的图普。 为什么这不起作用呢?

问题回答

您在模式上没有组合。 要在组合中捕捉某个东西, 您必须用括号围住它 :

([A-Z]{3}).[A-Z]{3}

例外是 m.group(0) , 它总是包含全部匹配 。

查看您的问题时, 似乎您并不实际寻找抓取组组, 而是重复匹配。 在正对数中, 一个组意味着留待以后使用的匹配中较小部分 。 例如, 如果您试图将电话号码和类似

([0-9]{3})-([0-9]{3}-[0-9]{4})

然后区域代码将位于 group(1) , 本地部分在 group(2) , 全部在 group(0)

您想要的是找到重叠匹配 。 < strong> 这里是一个堆叠溢流解解答, 解释了 < a href=> https:// stackoverflow.com/ a/ 5616910/ 399649> > 如何在 Python regex , 和这里我最喜欢的 < a href=" http://www. recional- 表达性. info/ brackets. html" rel= “ nofollow noreferrer” 中为抓获组和一般 Regex 引用的重复匹配 。

第一,您正在使用 match ,当您看起来想要 findall 时。 它不会抓住随附的资本三胞胎, 但 re.findall ([A-Z]{3})([a-z])(?=[A-Z]{3}] (?=[A-Z]{3}), 搜索_string) 将使您获得双侧所有单个小案件字符的3个上限。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签