繁体   English   中英

如何在数字出现正则表达式之前获取字符串的最后一部分?

[英]How do I get the last part of a string before a number shows up with regex?

我正在使用 Python 3.8 循环遍历列表中的每个字符串。 最后我只想提取 colors 。

似乎 colors 与模型名称除以日期(或至少一个带有一些数字的单词),我正在尝试使用 python 库“re”来拆分字符串。 但我不知道如何从头开始而不是从头开始?

Jackson_Phil_Demmel_Demmelition_King_V_2010s_Standard_Finish
Paul_Reed_Smith_513_2012_Black_Slate
Paul_Reed_Smith_408_TM_with_Artist_Package_2013_Whale_Blue
Paul_Reed_Smith_408_MT_2013_Livingston_Lemondrop
Paul_Reed_Smith_20th_Anniversary_12-string_with_10-top_2005_Yellow_Burst
Paul_Reed_Smith_Brent_Mason_Signature_2Epiphone_EA-255_Casino_1970s_Walnut
Epiphone_Limited_Edition_Wilshire_Pro_2000s
Epiphone_Coronet_1966_Cherry010s_Transparent_Finish
Epiphone_Matt_Heafy_Signature_7-String_Les_Paul_Custom
B.C._Rich_Mockingbird_late_80s_Black_Sparkle
GandL_ASAT_Classic_1990s_Solid_Finish_or_Sunburst

如何拆分这些字符串以仅保留最终结果?

'.*[0-9]s?_(.*)'类的东西应该可以工作,然后参加小组。

例如:

import re

for s in [
    'Jackson_Phil_Demmel_Demmelition_King_V_2010s_Standard_Finish',
    'Paul_Reed_Smith_513_2012_Black_Slate',
    'Paul_Reed_Smith_408_TM_with_Artist_Package_2013_Whale_Blue',
    'Paul_Reed_Smith_408_MT_2013_Livingston_Lemondrop',
    'Paul_Reed_Smith_20th_Anniversary_12-string_with_10-top_2005_Yellow_Burst'
    'Paul_Reed_Smith_Brent_Mason_Signature_2Epiphone_EA-255_Casino_1970s_Walnut',
    'Epiphone_Limited_Edition_Wilshire_Pro_2000s',
    'Epiphone_Coronet_1966_Cherry010s_Transparent_Finish',
    'Epiphone_Matt_Heafy_Signature_7-String_Les_Paul_Custom',
    'B.C._Rich_Mockingbird_late_80s_Black_Sparkle',
    'GandL_ASAT_Classic_1990s_Solid_Finish_or_Sunburst']:

    m = re.match('.*[0-9]s?_(.*)',s)
    color = m.groups()[0] if m else 'NONE'

    print(f'{s}:\t{color}')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM