regex - Python Regular Expression help. Combinations -
I am trying to read from a text file and am preparing a list of seed words that start a sentence And a second list, all the adjacent words except the seed words.
The problem is that the words containing an apostrophe are split after the apostrophe and the remaining words are omitted. How will you keep them in the file?
Text contained in the file: Code: results: With two regex it's easy:
Is it not a search or all correct approach to work? Or neither?
my_string = open ('sample.txt', 'r'). Read () starter = list (set (re.findall) (r "(? & Lt ;; [az] \ s) [az] \ w +", my_string) adjacent = redundant (r? (? Lt;; (? & Lt ;; [az] \ s)) \ W + ", my_string print (my_string)
['Doesn', 'look', 'to', 'work', 'sub', 'or', 'search', 'the', 'right', 'approach', 'n']
import re txt = "" \ \ It does not seem to work. Findall or sub-right approach? Or neither? Is not it grand? "" First_desk = set (again (''? (?: ^ | (?: [.?!] \ S)) (\ b [a-zA-z \ '] +)', txt) = rest = {In re.findall for word (r '(\ b [a-zA-z \'], txt) if the word is not printed in first_words} first_words # set (['this',' is', 'or ',' Is not ']) print rest # set (["no",' sub ',' grand ',' d ',' work ' This', 'search', 'to', 'no', 'right', 'sound', 'attitude'
Comments
Post a Comment