Hi Bináris,

I would consider it on a case-by-case basis. Note that the guidelines state "should be avoided", not "are absolutely forbidden to be used" :-)

For short regexps, I'd use f-strings where reasonable (i.e. where the regex itself does not use {} for the number of characters to match) and otherwise %-based formatting.

For longer regexps, string concatenation can often be clearer, also because it makes it possible to add comments to clarify how the regexp is built up.

Hope this helps,

Merlijn / valhallasw


On 11/02/2023 22:10, Bináris wrote:


Bináris <wikiposta@gmail.com> ezt írta (időpont: 2023. febr. 2., Cs, 19:22):
" Prefer f-strings over string.format(). Modulo operator % for string formatting should be avoided."

I tried to rewrite a modulo-formatted regex to f-string, but than realized, that in f-strings all curly braces must be doubled, which makes regexes very hard to read and easy to misspell.

What is the best practice when you substitute a variable into a regex?

Is there any policy for this? Or should there be? If not, and modulo is forbidden, I think the best tapproach is to use concatenation in such situation. Opinions?

--
Bináris

_______________________________________________
pywikibot mailing list -- pywikibot@lists.wikimedia.org
Public archives at https://lists.wikimedia.org/hyperkitty/list/pywikibot@lists.wikimedia.org/message/5T4HUQRFKWAOHANQD5LHPC776FIVLAPE/
To unsubscribe send an email to pywikibot-leave@lists.wikimedia.org