英文:
search for two identical characters and filter them on python
问题
如何执行这项任务更加优化。各种数据传送给我。示例数据:test/123, test, test/, test/123/,我需要将正确的数据写入我的数据库,但首先我需要找到它们。正确的数据将采用 test/123/
的格式,然后将它们分成变量。a = test,b = 123。告诉我如何完成这个任务?数据可以是任何大小。
英文:
how this task can be carried out is more optimized.
Various data come to me.
Example data : test/123, test , test/, test/123/
and I need to write the correct data to my database, but first I need to find it. The correct ones will be in the test/123/
format
and then divide them into variables
a = test
b = 123
Tell me how it can be done ?
the data can be of any size
答案1
得分: 0
import re
data = 'test/123/'
m = re.match(r'(\w+)/(\d+)/', data)
if m:
a = m[1]
b = m[2]
print(f'Found a = {a}, b = {b}')
else:
print('no match')
有关模式的更多调整,请参考Python的正则表达式 HOWTO。
英文:
This answers for a data set of one or more letters ("test" in the example above) and one or more digits ("123" in the example above)
import re
data = 'test/123/'
m = re.match(r'(\w+)/(\d+)/', data)
if m:
a = m[1]
b = m[2]
print(f'Found a = {a}, b = {b}')
else:
print('no match')
For more tweaks of the pattern, refer e.g. to python's Regular Expressions HOWTO
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论