Python Forum

Full Version: matching a repeating string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i have a single character (such as ".") in variable foo and another variable bar with a one or more repeat of a single character (such as "..."). i would like to know if foo matches all the repeats of bar. empty strings should never be considered to match. is this the best way to do that?
    if foo and bar and foo*len(bar) == bar:
        ...
Ifbarwill always be a single character repeated an number of times thenif foo in bar :would be the most straight forward test to see ifbarisfoorepeated.
no, bar could be some other value that may or may not be a repeating character. it may have an instance of foo within but if there is anything else in bar then i want a false result. bar="BashBedlam" and foo="B" needs to be false, but bar="BB" needs to be true when foo="B".