1

I'm trying to create a general regex based on string1. The regex should match any string2 which has the same order of characters as string1, but string2 could have other characters in between characters of string1, I'm only concerned about the order.

string = 'abc'
regexString = reduce(lambda char1,char2 : '\w*' + char1 + '\w*' + char2, string)
print regexString
\w*\w*a\w*b\w*c

I was expecting the regexString to be:

'\w*a\w*b\w*c'

I don't understand why there is an extra '\w*' at the start of regexString

Zizou
  • 67
  • 5

2 Answers2

3

Suppose I gave your anonymous function a name:

def f(char1, char2):
    return '\w*' + char1 + '\w*' + char2

What reduce is doing, in effect, is:

f(f('a','b')'c')

and so

f('a','b') returns \w*a\w*b, and thus f('\w*a\w*b', 'c') gives the result you're finding.

If you want to do this using reduce, I might suggest the following:

regexString = '\w*' + reduce(lambda char1,char2 : char1 + '\w*' + char2, string)
GuillaumeDufay
  • 1,118
  • 9
  • 13
  • 1
    FYI nitpick missing comma in your "doing in effect" explanation :) (I'll delete this comment after you fix) – Ray Toal Jul 21 '19 at 23:50
1

The other answer well covers the behavior of reduce. I'd like to suggest a more Pythonic solution to the original problem:

"\\w*" + "\\w*".join(iter(string))
Linuxios
  • 34,849
  • 13
  • 91
  • 116
  • 2
    This is great information for poeple as `reduce` is not favored in Python. See https://stackoverflow.com/q/181543/831878 for a quote from the creator of Python, in which he says he hates reduce "the most." :) – Ray Toal Jul 21 '19 at 23:48
  • Even this is a good and acceptable answer, I accepted the first comment as an answer as it not only pointed out what the answer was, but also helped me understand the reduce function better. Having said that, I will use this solution as it is a more pythonic solution. – Zizou Jul 22 '19 at 01:08
  • @Zizou: Absolutely! It's important that there be an answer that addresses the immediate question at hand; I just wanted to offer a more idiomatic solution for contrast. Glad to be able to help. – Linuxios Jul 22 '19 at 01:14