Python Regex punctuation recognition

2024/11/24 16:33:19

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_patternfrom re import findall
start_position = 0for poem in poemList:start_position = text.find('<' + poem + '>', start_position)end_position = text.find('</' + poem + '>', start_position)searchtext = text [start_position:end_position]poemname = poemfor 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.

Answer

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://en.xdnf.cn/q/120511.html

Related Q&A

cant find brokenaxes module

I want to create a histogram with broken axes and found that there must be a module doing this called brokenaxes that works together with matplotlib (source) . Anyway, when I trie to import the modul…

Python-Pandas While loop

I am having some trouble with the DataFrame and while loop:A B 5 10 5 10 10 5I am trying to have a while loop that:while (Column A < Column B):Column A = Column A + (Column B / 2)Colu…

split an enumerated text list into multiple columns

I have a dataframe column which is a an enumerated list of items and Im trying to split them into multiple columns. for example dataframe column that looks like this:ID ITEMSID_1 1. Fruit 12 oranges 2…

Python using self as an argument [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

how to shuffle questions in python

please anyone help me i have posted my whole work down can anyone tell me how to shuffle theses five questions it please i will be very thankful.print("welcome to the quiz") Validation = Fals…

Selenium python do test every 10 seconds

I am using selenium (python) testing and I need to test my application automatically every 10 seconds.How can I do this?

Type Object has no attribute

I am working on a program, but I am getting the error "Type object Card has no attribute fileName. Ive looked for answers to this, but none that Ive seen is in a similar case to this.class Card: R…

Generate random non repeating samples from an array of numbers

I made a battleships game and I now need to make sure that the computer doesnt attack at the same spot twice.My idea of it is storing each shots co-ordinates in a variable which gets added to whenever …

How to get back fo first element in list after reaching the end? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 5 years ago.Improve…

Collision detection on the y-axis does not work (pygame)

I am trying to get the collision detection in my code to work. I am using vectors and I want the player sprite to collide and stop when it collides with a sprite group called walls. The problem is that…