在Python中搜索两个相同的字符并对它们进行筛选。

huangapple go评论62阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年2月8日 20:18:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385687.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定