Question

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.

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top