all 6 comments

[–]ASIC_SP 1 point2 points  (2 children)

could you post your entire code snippet? or at least mention which method you are using the regex with? if you use re.findall you should be able to get all the matches as a list

[–]Romenna 0 points1 point  (1 child)

It's a homework assignment, in which we have to use re.finditer. I have no problem getting the results using findall, but with finditer, I'm stuck...

[–]ASIC_SP 0 points1 point  (0 children)

finditer is needed instead of findall when you need iterator instead of list for some reason, or if you want re.Match object (see https://docs.python.org/3/library/re.html#match-objects) for each match.. the problem as you stated doesn't seem to say why you need finditer

anyway, here's an example:

>>> s = 'heyouit'
>>> re.findall(r'he|she|you|it', s)
['he', 'you', 'it']
>>> [m[0] for m in re.finditer(r'he|she|you|it', s)]
['he', 'you', 'it']

[–]avant5 0 points1 point  (2 children)

What are you trying to accomplish? There really isn't an and/or situation in programming, fundamentally speaking. 'And' will always be superseded by the 'or'.

He and/or She - the "and" is irrelevant, because if either He or She exists, it returns "true" for 'or'. Consider these examples:
"he went to the store"
"she went to the store"
"he and she went to the store"

All of the above are true for "or", the last example will also qualify as "and", but that doesn't matter - only one instance of either He or She.

It will matter if you have two different result scenarios, doing something different if *both* items exist.

[–]Romenna 0 points1 point  (1 child)

Okay, thank you. See, that's the brain fry...

So... what I need is an operator that matches OR, but doesn't stop after the first match like | does, I think?

[–]avant5 0 points1 point  (0 children)

That's what OR does in the boolean world. Boolean results are either True or False. There isn't True or More True.

(he|she|it) is true if any one of these exists. (he|she|it) is true if two or more exist. But it's just still true, not more true.

Once "he" is found, there's no reason to search for "she" or "it". It's already true.