Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
527 views
in Technique[技术] by (71.8m points)

Python Regex punctuation recognition

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""
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 ("
")  

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

Any help would be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After minimizing your example we have:

re.findall(r"/,", "/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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...