如何为字符串提供占位符,使其与 python 中的其他字符串匹配
How to give a placeholder for string so that it matches with other in python
我正在做一个项目,我从客户那里得到一个字符串。我想将此字符串与 expected_string.
匹配
假设我的客户给出:your name is: Mudit, Hello
我希望我的 expected_string 像 your name is: <any_name>, Hello
我想assert client_string == expected_string
。
我想不出办法。
如果
client_string = "your name is Mudit"
和
expected_string = "your name is"
我本来可以去的
assert expected_string in client_string
但是如果变量在中间某处,该怎么做。我该如何断言。
为什么不检查 client_string 是否以 expected_string 开头?
其余的逻辑由您来实现。
https://www.w3schools.com/python/ref_string_startswith.asp
编辑:要使用模式,正则表达式:
import re
pattern = r'your name is (\w+) and your age is (\d+) years'
result = pattern.search(your_string)
valid = result is not None
if valid:
print(result.groups())
我正在做一个项目,我从客户那里得到一个字符串。我想将此字符串与 expected_string.
匹配假设我的客户给出:your name is: Mudit, Hello
我希望我的 expected_string 像 your name is: <any_name>, Hello
我想assert client_string == expected_string
。
我想不出办法。
如果
client_string = "your name is Mudit"
和
expected_string = "your name is"
我本来可以去的
assert expected_string in client_string
但是如果变量在中间某处,该怎么做。我该如何断言。
为什么不检查 client_string 是否以 expected_string 开头? 其余的逻辑由您来实现。 https://www.w3schools.com/python/ref_string_startswith.asp
编辑:要使用模式,正则表达式:
import re
pattern = r'your name is (\w+) and your age is (\d+) years'
result = pattern.search(your_string)
valid = result is not None
if valid:
print(result.groups())