Python3,re不适用于UserString吗?

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

在Python3中,我发现此意外的TypeError:

bpython version 0.17.1 on top of Python 3.6.9 /usr/bin/python3

import re
from collections import UserString

s = UserString('foo')
re_repetitions = re.compile(r"(/)\1{1,}", re.DOTALL)
re_repetitions.sub(r"\1", s)

Traceback (most recent call last):
  File "<input>", line 1, in <module>
    re_repetitions.sub(r"\1", s)
TypeError: expected string or bytes-like object

-为什么会这样?有标准的解决方法吗?

python regex
1个回答
1
投票

UserString的实例不是str的实例,但它contain

© www.soinside.com 2019 - 2024. All rights reserved.