re.search does not read all matches

2

I am making a regular expression to find all the words in a sentence. I have the following:

import re

emoji_pattern = re.compile('[A-Za-z]+')


print(emoji_pattern.search("jajaja que haces?"))

But when I run it, it gives me

<_sre.SRE_Match object; span=(0, 6), match='jajaja'>

that just says the word jajaja .

She is the only one who finds, but nevertheless she should give all the words.

    
asked by Luis Miguel 07.03.2018 в 05:18
source

2 answers

3

A simple option is to use re.findall () , such as described @ TheSupermax03 .

A more efficient option is to use re.finditer () , which generates a iterator, especially if it is a long text.

import re

regex = r"[A-Za-z]+"
texto = "jajaja que haces?"

for match in re.finditer(regex, texto):
    print( match.group(0) )
jajaja
que
haces


In addition, re.finditer () allows you to obtain the text captured by each group, in each coincidence.

* and works well with empty matches, where findall skips 1 character.

    
answered by 07.03.2018 / 16:41
source
3

What happens is that search returns the first match you find according to the regular expression you used, so in your case it returned jajaja . If you want to find all the occurrences you can use findall. Your code with findall would be as follows

import re

emoji_pattern = re.compile('[A-Za-z]+')
print(emoji_pattern.findall('jajaja que haces?'))

The output is as follows:

  ['jajaja', 'que', 'haces']

I include some links to visit:

link link

    
answered by 07.03.2018 в 06:41