简体   繁体   English

使用正则表达式查找型号

[英]find model number using regex

I have the following list, which I am trying to extract the item model number using regex我有以下列表,我正在尝试使用正则表达式提取项目型号

names=[
    'Honda Engine GX200 6.5HP 2.43" x 3/4" Crankshaft',
    'Honda New GX390 Engine Standard 1" Crank, Electric Start, Oil Alert',
    'Genuine Honda 79160-SHJ-A41 Temperature Driver Motor Assembly',
    'Auto Express Long Block Engine Crankcase with Cylinder Head Valves Fits Honda GX200 6.5 HP',
    'Honda 08207-10W30 PK2 Motor Oil'
]

The model number can only contain Upperletters,-,numbers型号只能包含大写字母、-、数字

for name in names:
    model_num=re.search('([A-Z]+\d+\-[A-Z]*)',name).groups()[0]

my regex isn't working all the time.我的正则表达式不能一直工作。 expected output is:预期输出为:

['GX200','GX390','79160-SHJ-A41','GX200','08207-10W30']

any help is much appreciated, if there is an easier way than regex that works too.如果有比 regex 更简单的方法,任何帮助都将不胜感激。

Usage of re.compile could improve speed a bit:使用re.compile可以稍微提高速度:

find_model = re.compile(
    """
    [A-Z\d\-]+
    (?![a-z])  # Check that next char isn't lowercase to avoid getting false-positive head letter only
    """,
    re.VERBOSE,
)
for name in names:
    result = find_model.search(name)
    if result:
        model_num = result.group(0)
        print(model_num)

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

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