簡體   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