gpt4 book ai didi

python - 将点分隔的字符串拆分为单词,但有一个特殊情况

转载 作者:太空狗 更新时间:2023-10-30 02:04:51 25 4
gpt4 key购买 nike

不确定是否有拆分以下字符串的简单方法:

'school.department.classes[cost=15.00].name'

进入这个:

['school', 'department', 'classes[cost=15.00]', 'name']

注意:我想保持 'classes[cost=15.00]' 完整。

最佳答案

>>> import re
>>> text = 'school.department.classes[cost=15.00].name'
>>> re.split(r'\.(?!\d)', text)
['school', 'department', 'classes[cost=15.00]', 'name']

更具体的版本:

>>> re.findall(r'([^.\[]+(?:\[[^\]]+\])?)(?:\.|$)', text)
['school', 'department', 'classes[cost=15.00]', 'name']

详细:

>>> re.findall(r'''(                      # main group
[^ . \[ ]+ # 1 or more of anything except . or [
(?: # (non-capture) opitional [x=y,...]
\[ # start [
[^ \] ]+ # 1 or more of any non ]
\] # end ]
)? # this group [x=y,...] is optional
) # end main group
(?:\.|$) # find a dot or the end of string
''', text, flags=re.VERBOSE)
['school', 'department', 'classes[cost=15.00]', 'name']

关于python - 将点分隔的字符串拆分为单词,但有一个特殊情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16827806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com