作者cphsu (kent)
看板Python
标题Re: [问题] 一行字串中如何找出第一个中文字
时间Wed Jul 22 22:07:30 2015
I'm not sure what's the encoding of your string, so I made two programs.
Use at your own risk. Especially the second pattern is not exactly the range of
Chinese characters.
refs:
https://zh.wikipedia.org/wiki/big5
https://zh.wikipedia.org/wiki/UTF-8
https://en.wikipedia.org/wiki/CJK_Unified_Ideographs
----------------------------------------------------------
# -*- coding: big5 -*-
import re
s = "x=643 y=17 text=回首页"
m = re.search('[\x81-\xfe][\x40-\x7e\xa1-\xfe]', s)
print m.group(0)
----------------------------------------------------------
# -*- coding: utf-8 -*-
import re
s = "x=643 y=17 text=回首页"
m = re.search('([\xc0-\xdf][\x80-\xbf]{1})|([\xe0-\xef][\x80-\xbf]{2})|([\xf0-\xf7][\x80-\xbf]{3})', s)
print m.group(0)
#print m.group(0).decode("utf-8").encode("big5")
※ 引述《pandadao (panda)》之铭言:
: 各位前辈好,最近小弟在练习写python,遇到一个难题,假设我现在有一行
: 文字,类似像 x=643 y=17 text=回首页,可是有时候text後面会有一些符号
: 像是括号或逗点之类的,像是x=643 y=17 text=(回首页) 要怎麽样才能在
: 保留前面座标轴资料的前提下找出第一个中文字呢?麻烦高手教教我,谢谢
: 我目前是用python2
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 211.72.92.133
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1437574054.A.7C7.html
1F:推 sa0124: 推 07/24 06:30
2F:推 pandadao: 谢谢您的帮助 07/26 17:24