社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Oliver Wienand  »  全部回复
回复总数  1

标准方法可能使用正则表达式,例如:

import re

structure_string = r"bob\|.*\|abc\|manual$"
test_structure = re.compile(structure_string)
incoming = [
    "bob|0|abc|manual",
    "bob|1|abc|manual",
    "bob|1|abc|manualX",
    "Xbob|1|abc|manual",
    "anne|1|abc|manual"
]

for incoming_string in incoming:
    print(incoming_string, test_structure.match(incoming_string))

assert test_structure.match(incoming[0]), "Not matching: %s ~ %s" % (structure_string, incoming_string)
assert test_structure.match(incoming[-1]), "Not matching: %s ~ %s" % (structure_string, incoming_string)

bob|0|abc|manual <re.Match object; span=(0, 16), match='bob|0|abc|manual'>
bob|1|abc|manual <re.Match object; span=(0, 16), match='bob|1|abc|manual'>
bob|1|abc|manualX None
Xbob|1|abc|manual None
anne|1|abc|manual None
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
[...]
---> 17 assert test_structure.match(incoming[-1]), "Not matching: %s ~ %s" % (structure_string, incoming_string)

AssertionError: Not matching: bob\|.*\|abc\|manual$ ~ anne|1|abc|manual

官方文件: https://docs.python.org/3/library/re.html#

很好的在线正则表达式工具: https://regex101.com/