Python Regex punctuation recognition

你说的曾经没有我的故事 提交于 2019-12-13 09:49:06

问题


I am stumped by this one. I am just learning regular expressions and cannot figure out why this will not return punctuation marks.

here is a piece of the text file the regex is parsing:

APRIL/NNP is/VBZ the/DT cruellest/JJ month/NN ,/, breeding/VBG Lilacs/NNP out/RB of/IN the/DT dead/JJ land/NN

text = open_file.read()

grammarList = raw_input("Enter your grammar string: ");
tags = grammarList.split("^")


tags_pattern = r'\s+'.join(r"([\w\,\:\;\"\-\.]+)/{0}".format(re.escape(tag)) for tag in tags) + r"\b"
print tags_pattern

from re import findall
start_position = 0

for poem in poemList:
    start_position = text.find('<' + poem + '>', start_position)
    end_position = text.find('</' + poem + '>', start_position)

    searchtext = text [start_position:end_position]
    poemname = poem
    for oldname, newname in poemtitleswapList.items():
        poemname = poemname.replace(oldname, newname)

    print (poemname)
    print (findall(tags_pattern, searchtext))
    print ("\n")  

I thought that in the square brackets the "\," would allow it to return a "," but it is not working.

Any help would be appreciated.


回答1:


After minimizing your example we have:

re.findall(r"/\,\b", "/NN ,/, breeding/VBG Lilacs/NNP out/RB of/IN the/DT dead/JJ land/NN")

And it does not match for obvious reasons: there is no beginning of the word immediately after the comma.



来源:https://stackoverflow.com/questions/9195599/python-regex-punctuation-recognition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!