Python regex - the difference between finding and finding all

I am trying to use python regex in a URL string.

id= 'edu.vt.lib.scholar:http/ejournals/VALib/v48_n4/newsome.html'
>>> re.search('news|ejournals|theses',id).group()
'ejournals'
>>> re.findall('news|ejournals|theses',id)
['ejournals', 'news']

Based on the docs at http://docs.python.org/2/library/re.html#finding-all-adverbs he says that search () matches the first and finds all possible matches in a string.

I wonder why 'news' is not captured by the search, even if it is declared first in the template.

Did I use the wrong template? I want to search if any of these keywords appears in a string.

+5
source share
3 answers

. , "news" "ejournals" "theses" . "ejournals" .

+1

re.search() , , .

+2

Remember that there are other differences between search and findall that are not listed here. For instance:

python-regex why findall doesn't find anything but search works?

0
source

All Articles