【问题标题】:RegEx for capturing Korean alphabets用于捕获韩语字母的正则表达式
【发布时间】:2019-05-24 03:02:06
【问题描述】:

我的数据框名称如下:

'가락시장(340)',
'가락시장(8)',
'가산디지털단지(7)',
'강남(222)',
'강남구청',
'강동',
'강동구청',
'강변(214)',
'개롱',
'개화산',
'거여',
'건대입구(212)',
'건대입구(7)',
'경복궁(317)',
'경찰병원(341)',
'고덕',
'고려대',
'고속터미널(329)',
'고속터미널(7)',
'공덕(5)',
'공덕(6)',
'공릉',
'광나루',
...

所有列表都在这个link

期望的输出:

가락시장
가산디지털단지
강남
강남구청
강동
강동구청
강변
개롱
개화산
거여
건대입구
경복궁
경찰병원
고덕
고려대
고속터미널
공덕
공릉
광나루

尝试

import re

for i in df['name']:
    i = re.match('^[가-힣]*$', i) # '^[가-힣]&$ is extract Hangeul (Korean alphabet)

但是df['name'] 没有改变。

我该如何解决这个问题?

【问题讨论】:

    标签: python regex regex-group cjk regex-greedy


    【解决方案1】:

    我们也许可以用一个简单的表达式来捕获你想要的输出,只有一个' 作为左边界,然后收集字母,类似于:

    '([\p{L}]+)
    

    测试

    # coding=utf8
    # the above tag defines encoding for this document and is for Python 2.x compatibility
    
    import re
    
    regex = r"'([\p{L}]+)"
    
    test_str = ("'가락시장(340)',\n"
        " '가락시장(8)',\n"
        " '가산디지털단지(7)',\n"
        " '강남(222)',\n"
        " '강남구청',\n"
        " '강동',\n"
        " '강동구청',\n"
        " '강변(214)',\n"
        " '개롱',\n"
        " '개화산',\n"
        " '거여',\n"
        " '건대입구(212)',\n"
        " '건대입구(7)',\n"
        " '경복궁(317)',\n"
        " '경찰병원(341)',\n"
        " '고덕',\n"
        " '고려대',\n"
        " '고속터미널(329)',\n"
        " '고속터미널(7)',\n"
        " '공덕(5)',\n"
        " '공덕(6)',\n"
        " '공릉',\n"
        " '광나루',")
    
    matches = re.finditer(regex, test_str, re.MULTILINE)
    
    for matchNum, match in enumerate(matches, start=1):
    
        print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
    
        for groupNum in range(0, len(match.groups())):
            groupNum = groupNum + 1
    
            print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
    
    # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
    

    正则表达式

    如果不需要此表达式,可以在 regex101.com 中修改或更改它。

    正则表达式电路

    jex.im 可视化正则表达式:

    参考

    How to implement \p{L} in python regex

    【讨论】:

      【解决方案2】:

      您可以使用以下代码删除括号和带括号的字符:

      import re
      pattern = re.compile(r'\(\w*\)')
      for text in YOUR_DATA_LIST : 
          only_station_name = re.sub(pattern, '', text)
          print(only_station_name)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-09-10
        • 1970-01-01
        • 2012-03-20
        • 1970-01-01
        • 2019-10-15
        • 2019-10-08
        • 1970-01-01
        相关资源
        最近更新 更多