1

Say we have:

f = "The object named '{}' is corrupted."
s = "The object named 'my_object' is corrupted."

s is equal to f.format("my_object"). If we didn't know that, how could we check if s can be generated by formatting f? Is there a function to do that in the standard library?

This can be done using regex but we'd have to reimplement the logic of how format works (e.g., curly braces are replaced with any string, etc). This is why I'd like to know if there's a way to use the same logic that str.format uses.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
LoneCodeRanger
  • 413
  • 6
  • 18
  • 2
    You can convert your formatting string into a regex automatically though, and then check if a particular string matches this template. Just replace `{}` with `.*` in the formatting string. It will be hard to support just ANY formatting syntax though – Alexey S. Larionov May 04 '23 at 10:35
  • I think this package, [`parse`](https://github.com/r1chardj0n3s/parse), answers my question. It's presented as "the opposite of format()". Also the fact that it exists points toward a gap in the std lib for that. – LoneCodeRanger May 04 '23 at 10:37
  • Or not, as the same argument could be made for *any* third party package. (OT) – mkrieger1 May 04 '23 at 10:38

0 Answers0