Python:正则表达式和字符串长度(以字节为单位)

Tu *_*ang 1 python regex string-length

我在python中编写一个程序并且有一些问题(我对python是100%新的):

import re

rawData = '7I+8I-7I-9I-8I-'

print len(rawData)

rawData = re.sub("[0-9]I\+","",rawData)
rawData = re.sub("[0-9]I\-","",rawData)

print rawData
Run Code Online (Sandbox Code Playgroud)
  1. 如何将2个正则表达式合并为一个|?这意味着它将摆脱两者9I-9I+仅使用一个正则表达式操作.
  2. len(rawData)返回rawData的长度是字节吗?

谢谢.

Fac*_*sco 5

看到不同:

$ python3
Python 3.1.3 (r313:86834, May 20 2011, 06:10:42) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> len('día')   # Unicode text
3
>>> 

$ python
Python 2.7.1 (r271:86832, May 20 2011, 17:19:04) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> len('día')   # bytes
4
>>> len(u'día')  # Unicode text
3
>>>


Python 3.1.3 (r313:86834, May 20 2011, 06:10:42) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> len(b'día')
  File "<stdin>", line 1
SyntaxError: bytes can only contain ASCII literal characters.
>>> len(b'dia')
3
>>> 
Run Code Online (Sandbox Code Playgroud)