我想在带有Flask的python项目中使用资源并输出它们的名称。我知道它在VB中是如何工作的。但我不知道在Python中My.Resources.ResourceManager的等价物是什么。Python中有相同的功能吗?
我想保存多个正则表达式家长,如下所示。我还想按名称在代码中使用它。
名称值Regex1(?Pnickname\s*.+?(Regex2(?Paddress\s*.+?(
欢迎来到SO!
从本质上讲,大多数时候你不需要担心python中的资源管理,因为它是自动为你完成的。因此,要保存正则表达式模式:
import re
# create pattern strings
regex1 = '(?P<nickname>s*.+?)'
regex2 = '(?P<address>s*.+?)'
test_string = 'nickname jojo rabbit.'
matches = re.search(regex1, test_string)
正如你可能注意到的,这里没有什么特别的。创建和存储这些模式就像声明任何字符串或其他类型的变量一样。
如果你想更整洁地保存所有的模式,你可以使用一个字典,其中模式的名称是键,模式字符串是值,比如:
import re
regex_dictionary = {'regex1':'(?P<nickname>s*.+?)'}
# to add another regex pattern:
regex_dictionary['regex2'] = '(?P<address>s*.+?)'
test_string = 'nickname jojo rabbit.'
# to access and search using a regex pattern:
matches = re.search(regex_dictionary['regex1'], test_string)
我希望这是有道理的!
阅读有关python正则表达式的更多信息:https://www.w3schools.com/python/python_regex.asp#matchobject
阅读有关python词典的更多信息:https://www.w3schools.com/python/python_dictionaries.asp
阅读有关python资源管理的更多信息:https://www.drdobbs.com/web-development/resource-management-in-python/184405999