Re: [問題] 一行字串中如何找出第一個中文字

看板Python作者 (kent)時間10年前 (2015/07/22 22:07), 編輯推噓2(200)
留言2則, 2人參與, 最新討論串2/2 (看更多)
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://www.ptt.cc/bbs/Python/M.1437574054.A.7C7.html

07/24 06:30, , 1F
07/24 06:30, 1F

07/26 17:24, , 2F
謝謝您的幫助
07/26 17:24, 2F
文章代碼(AID): #1LhwEcV7 (Python)
文章代碼(AID): #1LhwEcV7 (Python)