当前位置:首页 » 编程语言 » python正则分割

python正则分割

发布时间: 2022-03-08 08:00:00

python正则表达式匹配一个空格分隔的所有单词对

findall应该只能按着你的patten顺次往后找,所以出不来你想要的那种结果吧,倒不如直接用split分割然后写个循环输出你要的结果
import re

patt = r'\W+'
str1 = 'as jk jsd eqwe dsads'
reg = re.compile(patt)

res = reg.split(str1)
lst = []
for x in res:
if x:
length_flag = 0
else:
length_flag = 1
for x in range(0,len(res)-length_flag-1):
lst.append((res[x],res[x+1]))
print(lst)
输出结果就是你要的了
[('as', 'jk'), ('jk', 'jsd'), ('jsd', 'eqwe'), ('eqwe', 'dsads')]
我用的python3所以print带了括号,
另外我是个没啥基础刚自学的 方法可能非常笨拙

② python 正则 分割中文的问题。。

基本思路是对的,那个正则表达式写错了。给你改一下,

>>>s=u'我是一个我二你我四呵呵你好啊我在操场上'
>>>printre.split(u'(?isu)我[是在二四]',s)[0]

正则表达式串也应该是unicode的。另外加上(?isu), 这样表示忽略大小,可以多行搜索,可以搜索unicode

③ 用python,正则或其他方法,分割字符串

a="""cellId=0xd,ueId=0xd0000,ueIndex=0x0,crnti=0x54e5
trId=6815744ueId=851968crnti=21733hoType=0numOfSrb=1numOfDrb=0poolId=0
cellId=0xd,ueId=0xd0002,ueIndex=0x10,crnti=0x5567"""
#先格式处理,将'=',换行符和''统一使用逗号分隔
a=a.replace("",",").replace("=",",").replace(" ",",")
#过滤无效的值
b=filter(None,a.split(","))
#转换成字典
d=dict(zip(b[0::2],b[1::2]))

d就是你要的字典,不过你这给出的可是有重复的键呢,比如ueId,它就是重复的,重复的取最后一次覆盖的值,

④ python正则利用中文分割

结果哪里一样了啊

第一条命令使用中文字符作为分隔符,得到的是数字

第二条命令使用数字作为分隔符,得到的是中文字符

⑤ python中分割字符串

imkow正解,直接转list最好,否则自己写list comprehension其实隐含的还是把字符串当list用,多此一举

⑥ python正则表达式的分割

result=re.split(pattern,val,1)
print(result)

⑦ python 字符串分割截取

>>>
10.1.1.100,10.2.100.125,189.10.2.40,189.0.100.50
10.1.1.1,10.2.100.1,189.10.2.1,189.0.100.1str='10.1.1.100,10.2.100.125,189.10.2.40,189.0.100.50'
print(str)
str=str.split(',')
str1=[]
forelementinstr:
list=element.split('.')
list[len(list)-1]='1'
list='.'.join(list)
str1.append(list)
str1=','.join(str1)
print(str1)

⑧ python正则分割字符串的问题

按照你的要求编写的python程序如下(先把小括号外的逗号,用sub函数替换成连字符-,再根据连字符-用split函数切分成字符串数组)

#!/usr/bin/python
importre

a='(1,2),(3,4),(5,5)'
s=re.sub(r',(?=[^)]*((|$))','-',a)
print's=',s
result=re.split('-',s)
print(result)

运行结果
s=(1,2)-(3,4)-(5,5)
['(1,2)','(3,4)','(5,5)']

⑨ python 怎么将字符串分割

固定长度分割,直接通过[:3] 这种来取。
固定分隔符一般用split
看你需求,其他的方式也有。最好有个例子。

⑩ python字符串分割

格式太乱了,给你个参考吧
import re
s = 'type <unsigned int>\nport_num:4\nport:<in_port><sc_out<sc_uint<4>>>'
a = s.split('\n')
ok = []
for i in a:
if re.match('>',i[len(i)-1]):
print(i[:len(i)-1].replace('><',',').replace(':<',',').replace(' <',','))
else:
print(i.replace(':',','))

热点内容
随机启动脚本 发布:2025-07-05 16:10:30 浏览:528
微博数据库设计 发布:2025-07-05 15:30:55 浏览:25
linux485 发布:2025-07-05 14:38:28 浏览:305
php用的软件 发布:2025-07-05 14:06:22 浏览:756
没有权限访问计算机 发布:2025-07-05 13:29:11 浏览:433
javaweb开发教程视频教程 发布:2025-07-05 13:24:41 浏览:707
康师傅控流脚本破解 发布:2025-07-05 13:17:27 浏览:243
java的开发流程 发布:2025-07-05 12:45:11 浏览:687
怎么看内存卡配置 发布:2025-07-05 12:29:19 浏览:285
访问学者英文个人简历 发布:2025-07-05 12:29:17 浏览:835