1

I have an array of strings that I want to match in python. Is there a way to do this without using for loops?

All the examples I have seen so far are like the following, where you have to loop through each element to find a match:

import re

patterns = [ 'this', 'that' ]
text = 'Does this text match the pattern?'

for pattern in patterns:
    print 'Looking for "%s" in "%s" ->' % (pattern, text),

    if re.search(pattern,  text):
        print 'found a match!'
    else:
        print 'no match'

Would it be possible to do this without using a for loop

2
  • 3
    What's wrong with using a for loop? Commented Dec 12, 2014 at 0:40
  • If text is one word, it means my answer is correct.If text is more than one word than its impossible to do it without for loop. Commented Dec 12, 2014 at 1:29

4 Answers 4

3

Not exactly the same as your for loop, but concatenate the patterns with a |. a|b matches if either a or b matches.

ultimate_pattern = '|'.join(patterns)

If you want to get all matches, use findall, but this way it cannot be known which original pattern triggered the match for it returns a list of strings.

re.findall(ultimate_pattern, text)
Sign up to request clarification or add additional context in comments.

1 Comment

This is a very clever solution to the problem IMHO.
0

Perhaps you are looking for something like this:

import re

patterns = [ 'this', 'that' ]
text = 'Does this text match the pattern?'

if any(re.search(x, text) for x in patterns):
    print 'found a match!'
else:
    print 'no match'

It uses any and a generator expression to test every pattern in patterns against text. As an added bonus, the solution uses lazy evaluation and will only test as many patterns as is necessary.

2 Comments

You used for loop.It must be without for loop.
No I didn't. A for loop is what the OP has in their example. I used a generator expression, which just happens to have the same for and in keywords. Otherwise, what the OP wants is impossible since you need to iterate over the patterns list no matter what you do. Even using map uses an implicit for-loop under the hood.
0

Could you use a while loop like so?

patterns = [ 'this', 'that' ]
text = 'Does this text match the pattern?'
test = True
position = 0

while test == True:
  if patterns[position] in text:
      print(patterns[position],'is in the text')

  else:
      print (patterns[position],'is not in text')

  position = position+1
  if position >= len(patterns):
      test = False

Comments

0

import re

patterns = [ 'this', 'that' ]

text = 'Does this text match the pattern?'

def pattern_checker(patterns):

    pattern = iter(patterns)
    if re.search(next(pattern),  text):
            print 'found a match!'
            pattern_checker(pop_patterns(patterns))
    else:
            print 'no match'
            pattern_checker(pop_patterns(patterns))

def pop_patterns(patterns):

    if len(patterns) > 0:
            del patterns[0]
            return patterns
    else:
            return 0

try:

    pattern_checker(patterns)

except Exception, e:

    pass

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.