Spread a statement over various lines

Alexandre Brault abrault at mapgears.com
Wed Sep 18 16:24:28 EDT 2019


On 2019-09-18 4:01 p.m., Ralf M. wrote:
> Am 17.09.2019 um 20:59 schrieb Manfred Lotz:
>> I have a function like follows
>>
>> def regex_from_filepat(fpat):
>>      rfpat = fpat.replace('.', '\\.') \
>>                        .replace('%', '.')  \
>>                        .replace('*', '.*')
>>
>>      return '^' + rfpat + '$'
>>
>>
>> As I don't want to have the replace() functions in one line my
>> question is if it is ok to spread the statement over various lines as
>> shown above, or if there is a better way?
>>
>> Thanks.
>>
>
> Not related to your question, but:
> You seem to try to convert a Windows wildcard pattern to a regex
> pattern. However, wildcards sometimes behave a bit different than what
> you assume. I know for instance that *.* matches any filename, even if
> the filename doesn't contain a dot.
>
> Out of curiosity I played around a bit, details below.
> As you can see, there are other wildcard strangenesses, e.g.
> - ? does not match a dot
> - ???? between letters etc. matches exactly 4 characters, but
>   ???? at the end or directly before a dot matches at most 4 characters
>
> I don't know the exact rules of Windows wildcards, so there may be
> even more cases of unexpected behavior.
> If anyone knows where to find the complete rules (or a python module
> that implements them), I would be interested.
>

fnmatch in the standard library has a translate function that transforms
a glob pattern to a regex

https://docs.python.org/3.7/library/fnmatch.html#fnmatch.translate

Alex




More information about the Python-list mailing list