尝试使用Python将字符串拆分为多个部分

Ans*_*Ans 1 python unicode split

我试图以下面的方式分割字符串.这是一个示例字符串:

"Hello this is a string.?-2.34 This is an example1 string."
Run Code Online (Sandbox Code Playgroud)

请注意,""是U + F8FF unicode字符,字符串的类型是Unicode.

我想打破字符串:

"Hello this is a string.","-2.34"," This is an example1 string."
Run Code Online (Sandbox Code Playgroud)

我写了一个正则表达式来分割字符串,但使用这个我不能得到我想要的数字部分.(第一个字符串中的-2.34)

我的代码:

import re
import os
from django.utils.encoding import smart_str, smart_unicode

text = open(r"C:\data.txt").read()
text = text.decode('utf-8')
print(smart_str(text))

pat = re.compile(u"\uf8ff-*\d+\.*\d+")
newpart = pat.split(text)
firstpart = newpart[::1]

print ("first part of the string ----")
for f in firstpart:
f = smart_str(f)
print ("-----")
print f
Run Code Online (Sandbox Code Playgroud)

unu*_*tbu 5

-*\d+\.*\d+如果要将括号保留在以下结果中,则需要括起来re.split:

import re
text = u"Hello this is a string.\uf8ff-2.34 This is an example1 string."
print(re.split(u'\uf8ff(-*\d+\.*\d+)', text))
Run Code Online (Sandbox Code Playgroud)

产量

[u'Hello this is a string.', u'-2.34', u' This is an example1 string.']
Run Code Online (Sandbox Code Playgroud)