Python regex match if surrounded -


i try regex consist length of specific word between personal pronouns , '?' findall. did research didn't found how check if string start ... , ends ... there ?

edit: here exemple : have long text, want find how many times word "crazy" asked question :

are crazy ? -> match because there personal pronouns , '?' between word crazy ? -> no, because word between verb , '?' crazy ? -> match because there personal pronouns , '?' between word 

that should trick if don't want match "you crazy ?" form:

>>> import re >>> pat = '(?:i|you|he|she|it|we|they|me|him|her|it|us|them) (?:an? )?(\w\w+)\s?\?' >>> re.findall(pat, "is available ? isn't jerk ?") ['available', 'jerk'] 

otherwise, might work :

>>> import re >>> pat = '(?:i|you|he|she|it|we|they|me|him|her|it|us|them)((?: [a-z]+)+)\s?\?' >>> filt = re.findall(pat, "is available ? isn't jerk ? crazy ?") >>> filt [' available', ' jerk', ' crazy'] #then number of times crazy appeared in question : >>> len([el el in filt if "crazy" in el]) 1 

for second method, 2 filter, 1 extract between personal pronoun , "?". filter second time count how many times target word in these question forms. latter propper way use regex, cause actual dirty way count "notsocrazy" match


Comments