简体   繁体   English

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

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

I'm using Python 3.8 to loop through each string in a list.我正在使用 Python 3.8 循环遍历列表中的每个字符串。 I want to extract only the colors at the end.最后我只想提取 colors 。

As it seems that the colors are divided from the model's names by a date (or at least a word with some digits in it), I'm trying to use the python librairy 're' to split the strings.似乎 colors 与模型名称除以日期(或至少一个带有一些数字的单词),我正在尝试使用 python 库“re”来拆分字符串。 But I can't figure out how to start from the end rather than the beginning?但我不知道如何从头开始而不是从头开始?

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

how do I split those strings to only keep the end result?如何拆分这些字符串以仅保留最终结果?

Something like '.*[0-9]s?_(.*)' should work, then take the group. '.*[0-9]s?_(.*)'类的东西应该可以工作,然后参加小组。

eg:例如:

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.

相关问题 在数字正则表达式 python 之后获取最后一部分 - Get last part after number regex python 如何在某个字符之前获取字符串的最后一部分? - how to get the last part of a string before a certain character? 如何使用正则表达式 python 获取字符串前的最后两个数字? - How can I get the last two numbers before a string with regex python? 如何使用正则表达式从字符串中获取特定数字部分 - How to get a specific number part from a string using regex 如何在python中使用正则表达式获取字符串(数字和字符的混合)到最后一位? - How can I get a string(Mix of digit and char) up to last digit using regex in python? 如何计算日期在列表中显示的次数? - How do I count the number of times a date shows up in a list? 使用 pyspark 中的正则表达式将数字添加到字符串中最后一个字符之前的字符串 - Add number to a string before the last character in the string using regex in pyspark 如何从某些路径的最后一部分的组合中获取字符串? - How I get the string from the combination of the last part of some paths? 如何在Python中使用RegEx获得匹配字符串的一部分? - How could I get a part of a match string by RegEx in Python? 如何获得此字符串的正确正则表达式? - How do I get the correct regex for this string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM