文章詳情頁
java - 正則表達式問題
瀏覽:111日期:2023-11-01 15:25:44
問題描述
想用正則提取如下信息 改怎么寫呢?
123 男 北京 張三343 女 河北 石家莊 李四2343 男 山東 王五提取 男 張三 女 李四 男 王五
問題解答
回答1:其實對于中文, 特別是這樣格式的中文, 我是不建議用正則的, 雖然勉強也能實現:
# coding: utf8import refilename = ’2.txt’patern = re.compile(r’^d+ (S+).*?(S+)’)with open(filename) as f: for i in f:result = patern.findall(i[:-1]) if result and len(result[0]) == 2: print result[0][0], result[0][1] # 輸出:男 北京女 河北男 山東
你也可以用split的方法(建議):
# coding: utf8filename = ’2.txt’with open(filename) as f: for i in f:result = i.split()print result[1], result[-1] # 輸出:男 北京女 河北男 山東
標簽:
java
相關文章:
1. docker-machine添加一個已有的docker主機問題2. css - 關于div自適應問題,大家看圖吧,說不清3. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””4. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?5. angular.js使用$resource服務把數據存入mongodb的問題。6. dockerfile - [docker build image失敗- npm install]7. docker-compose中volumes的問題8. python - django如何每次調用標簽的時候都取隨機數據9. linux - mysql源碼安裝遇到的問題10. javascript - Vue 自定義控件v-model雙向綁定
排行榜
