- 2

please i need help i tried using the re mod to print the text in the var excluding the title but my code printed an empty list

import re word = """ title="hello how are you doing" """ find_word = re.findall(r"title\=([a-z])", str(word)) print(find_word)

4th Oct 2020, 7:26 PM
Victorr
Victorr - avatar
13 Answers
+ 4
Ruba Kh It will work as long as there are no more double quotes after the matching pattern in the docstring. For example, if the docstring were word = """ title="hello how are you doing" subject="something" """ the whole substring from title to the final " is matched.
4th Oct 2020, 8:32 PM
Russ
Russ - avatar
+ 3
Ruba Kh You could use the lazy quantifier instead though - *? (just thought of that) to make find_word = re.findall(r"title\=\"(.*?)\"", str(word))
4th Oct 2020, 8:35 PM
Russ
Russ - avatar
+ 2
The string says 'title="hello...' whereas the regex pattern misses out the double quotation marks after the '='. Change to find_word = re.findall(r"title\=\"([a-z])", str(word)) # ['h']
4th Oct 2020, 7:33 PM
Russ
Russ - avatar
+ 2
Ruba Kh 's answer will certainly work for your example where all characters are either a lower case letter or a space, but you may want to capture more complicated strings than those, in which there is a simple solution. After the 'title="', you want to match any character that is not a '"'. This is done easily using the ^ metacharacter which matches anything except the ones you include in the character class. find_word = re.findall(r"title\=\"([^\"]*)", str(word))
4th Oct 2020, 8:19 PM
Russ
Russ - avatar
+ 2
Victorr Ruba Kh 's answer worked fine, you probably missed the space in the brackets [a-z ].
4th Oct 2020, 8:28 PM
Russ
Russ - avatar
+ 1
Maybe you want something like that find_word = re.findall(r"title\=\"([a-z ]*)\"", str(word))
4th Oct 2020, 8:08 PM
Ruba Kh
Ruba Kh - avatar
+ 1
You are absolutely right I was just adjusting on the example I did not try to generalize the answer
4th Oct 2020, 8:22 PM
Ruba Kh
Ruba Kh - avatar
+ 1
Russ, Thanks so much sir i really appreciate you're d best!!!
4th Oct 2020, 8:24 PM
Victorr
Victorr - avatar
+ 1
Ruba Kh sir yours worked but it only printed hello thanks though really appreciate you guys for the help.
4th Oct 2020, 8:25 PM
Victorr
Victorr - avatar
0
Sir it worked buh it still got a problem it only printed the first letter "h" it didn't print the whole sentence "hello how are you doing"
4th Oct 2020, 7:56 PM
Victorr
Victorr - avatar
0
It can be done also like that right? Russ find_word = re.findall(r"title\=\"(.*)\"", str(word))
4th Oct 2020, 8:28 PM
Ruba Kh
Ruba Kh - avatar
0
Russ you are right sir it worked when i added the space THANKS
4th Oct 2020, 8:32 PM
Victorr
Victorr - avatar
0
Russ Ah thanks alot now I understand
4th Oct 2020, 8:33 PM
Ruba Kh
Ruba Kh - avatar