Home>

I want to unify "must say" and "must say" to "must say".
I tried it with the regex below, but it didn't work at all.

test_sentense = ["You must have wanted to say you can win",
                 "You can say you can win."]
test_p = ["Must have wanted to say", "Must say"]

END_SENTENSE_PATTERN = r "[{0}] | [{1}]". format (test_p [0], test_p [1])
print (re.sub (END_SENTENSE_PATTERN, "must say", test_sentense [0]))

Thanks for your cooperation.

  • Answer # 1

    import re
    test_sentense = ["You must have wanted to say you can win",
                     "You can say you can win."]
    test_p = ["Must have wanted to say", "Must say"]
    print (test_sentense [0])
    print (re.sub (test_p [0], test_p [1], test_sentense [0]))
    print (test_sentense [0] .replace (test_p [0], test_p [1]))


    You don't need a regular expression, can you just replace it?

  • Answer # 2

    Is the import of the re module missing?

    import re

Related articles