Heim  >  Fragen und Antworten  >  Hauptteil

python 正则表达式提取

41. gnos, gnit, gniz [L, GK]= to know 知

李平武. 英语词根与单词的说文解字(修订版) (Kindle 位置 3397-3398). 外语教学与研究出版社. Kindle 版本.

42. grad, gress [L]= to step 步

李平武. 英语词根与单词的说文解字(修订版) (Kindle 位置 3448-3449). 外语教学与研究出版社. Kindle 版本.

有如上内容,怎么样才可以通过正则表达式仅抓取数字那一行的内容,而忽略掉关于来源的内容呢?

谢谢!

PHP中文网PHP中文网2741 Tage vor531

Antworte allen(1)Ich werde antworten

  • PHPz

    PHPz2017-04-18 10:27:33

    import re
    
    regex = r"^\d.*"
    
    test_str = ("41. gnos, gnit, gniz [L, GK]= to know 知\n\n"
        "李平武. 英语词根与单词的说文解字(修订版) (Kindle 位置 3397-3398). 外语教学与研究出版社. Kindle 版本.\n\n"
        "42. grad, gress [L]= to step 步\n\n"
        "李平武. 英语词根与单词的说文解字(修订版) (Kindle 位置 3448-3449). 外语教学与研究出版社. Kindle 版本.")
    
    matches = re.finditer(regex, test_str, re.MULTILINE)
    
    for match in matches:
    
        
        print (match.group())
        

    Antwort
    0
  • StornierenAntwort