如何使用Rex查找<=AAA in Python?

问题描述 投票:0回答:1

我有以下代码

def append_letter():
  string = 'ACAABAACAAABACDBADDDFSDDDFFSSSASDAFAAACBAAAFASD'

  result = []
  # compete the pattern below
  pattern = r'(?<=AAA)\w+'
  for item in re.finditer(pattern, string):
    # identify the group number below.
    result.append(item.group(1))
  return result

如何附加未包含的字母 A 字母

从上面的脚本中,我想将任何字母后跟三个 A(包括 A)添加到列表中。

如何实现这个目标

python regex
1个回答
0
投票

你可以使用

re.findall
:

>>> import re
>>> str = 'ACAABAACAAABACDBADDDFSDDDFFSSSASDAFAAACBAAAFASD'
>>> re.findall(r'.(?:AAA)', str)
['CAAA', 'FAAA', 'BAAA']
>>> [match[0] for match in re.findall(r'.(?:AAA)', str)]
['C', 'F', 'B']
© www.soinside.com 2019 - 2024. All rights reserved.