首页 > 解决方案 > How can I extract English characters from a string of mixed (Arabic and English) Letters

问题描述

I have many strings similar as below

"A ali - عالي"
"Baghdad - بغداد"

I want to extract "A ali", "Baghdad" out of this using python but not splitting based on "-".

Please help.

标签: pythonselenium

解决方案


使用正则表达式。

前任:

import re
s = """A ali - عالي
Baghdad - بغداد"""

for line in s.splitlines():
    m = re.search(r"([A-Za-z\s]+)", line)
    if m:
        print(m.group(1))

输出:

A ali 
Baghdad 

推荐阅读