- 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)
13 odpowiedzi
+ 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.
+ 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))
+ 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']
+ 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))
+ 1
Maybe you want something like that
find_word = re.findall(r"title\=\"([a-z ]*)\"", str(word))
+ 1
You are absolutely right I was just adjusting on the example I did not try to generalize the answer
+ 1
Russ, Thanks so much sir i really appreciate you're d best!!!
+ 1
Ruba Kh sir yours worked but it only printed hello thanks though really appreciate you guys for the help.
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"
0
It can be done also like that right?
Russ
find_word = re.findall(r"title\=\"(.*)\"", str(word))
0
Russ you are right sir it worked when i added the space THANKS
0
Russ Ah thanks alot now I understand