吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 702|回复: 14
收起左侧

[求助] python正则遇到汉字怎么办?

[复制链接]
随遇而安8 发表于 2021-12-5 16:11
本帖最后由 随遇而安8 于 2021-12-5 16:14 编辑

代码如下,正则如果只匹配user_id":(.*?),"fullname":"(.*?)"是可以匹配出所有,但是再往后加上start_time": (.*?),匹配就为空
问题:是不是因为中间隔着“计算机课”这几个汉字,所以才不能匹配出start_time的吗?
[Python] 纯文本查看 复制代码
import re

response = '{"teacher_id":5114,"attendance_id":7727052,"user_id":16786743,"fullname":"张三","lesson_name":"计算机课","lesson_start_time":1633860985,"lesson_end_time":1633862410,"conversion_status":1},{"teacher_id":5114,"attendance_id":8062199,"user_id":12956827,"fullname":"李四","lesson_name":"计算机课","lesson_start_time":1638620201,"lesson_end_time":1638622728,"conversion_status":1},{"teacher_id":5114,"attendance_id":7961271,"user_id":12769816,"fullname":"王五","lesson_name":"计算机课","lesson_start_time":1637061349,"lesson_end_time":1637064881,"conversion_status":1}'
student = re.findall(
    'user_id":(.*?),"fullname":"(.*?)".*start_time": (.*?),"lesson_end_time', response)
print(student)
444.png
333.png

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

cszcszv163 发表于 2021-12-5 16:28
[Python] 纯文本查看 复制代码
import re

response = '{"teacher_id":5114,"attendance_id":7727052,"user_id":16786743,"fullname":"张三","lesson_name":"计算机课","lesson_start_time":1633860985,"lesson_end_time":1633862410,"conversion_status":1},{"teacher_id":5114,"attendance_id":8062199,"user_id":12956827,"fullname":"李四","lesson_name":"计算机课","lesson_start_time":1638620201,"lesson_end_time":1638622728,"conversion_status":1},{"teacher_id":5114,"attendance_id":7961271,"user_id":12769816,"fullname":"王五","lesson_name":"计算机课","lesson_start_time":1637061349,"lesson_end_time":1637064881,"conversion_status":1}'
student = re.findall(
    'user_id":(.*?),"fullname":"(.*?)".*?start_time":(.*?),"lesson_end_time', response)
print(student)

免费评分

参与人数 1吾爱币 +1 收起 理由
随遇而安8 + 1 用心讨论,共获提升!

查看全部评分

SDU123 发表于 2021-12-5 16:25
smldhz 发表于 2021-12-5 17:13
json唉 干嘛用正则啊
[Python] 纯文本查看 复制代码
import json
response = '{"teacher_id":5114,"attendance_id":7727052,"user_id":16786743,"fullname":"张三","lesson_name":"计算机课","lesson_start_time":1633860985,"lesson_end_time":1633862410,"conversion_status":1},{"teacher_id":5114,"attendance_id":8062199,"user_id":12956827,"fullname":"李四","lesson_name":"计算机课","lesson_start_time":1638620201,"lesson_end_time":1638622728,"conversion_status":1},{"teacher_id":5114,"attendance_id":7961271,"user_id":12769816,"fullname":"王五","lesson_name":"计算机课","lesson_start_time":1637061349,"lesson_end_time":1637064881,"conversion_status":1}'
json_str = '['+response+']'
json_obj = json.loads(json_str)
for data in json_obj:
    print("user_id:%d\tfullname:%s\tstarttime:%d"%(data['user_id'],data['fullname'],data['lesson_start_time']))

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
summiter + 1 + 1 用心讨论,共获提升!

查看全部评分

cszcszv163 发表于 2021-12-5 16:25
多打了空格
Snipaste_2021-12-05_16-24-32.png
cszcszv163 发表于 2021-12-5 16:35
这里要用懒惰匹配
Snipaste_2021-12-05_16-34-24.png

免费评分

参与人数 1热心值 +1 收起 理由
随遇而安8 + 1 谢谢@Thanks!

查看全部评分

lgsp_Jim 发表于 2021-12-5 16:25
*start_time",写错了吧
 楼主| 随遇而安8 发表于 2021-12-5 16:29

只匹配出来一条,如图
555.png
冥界3大法王 发表于 2021-12-5 16:31
@随遇而安8

中文编码范围,中文汉字的正则也许用的着。
双字节字符编码范围:
1. GBK (GB2312/GB18030)
\x00-\xff GBK双字节编码范围
\x20-\x7f ASCII
\xa1-\xff 中文gb2312
\x80-\xff 中文 gbk

2. UTF-8 (Unicode)
\u4e00-\u9fa5 (中文)
\x3130-\x318F (韩文)
\xAC00-\xD7A3 (韩文)
\u0800-\u4e00 (日文)
如果想在Android Studio 中查找汉字
可以使用正则搜索
[\u4e00-\u9fa5]

GiaoMan-wei 发表于 2021-12-5 16:35
请完整分析cszcszv16的代码,,人家的没问题
aspweb17 发表于 2021-12-5 16:55
又可以匹配的啊,,,赶紧搜下下
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2022-5-18 17:36

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表