0

I need to separate the brand from the model of a named car using a array of brands to match. I tried something like that:

import re

carName = "Fiat Strada Working 1.4 CS"

brands = ["Acura","Alfa Romeo","Aston Martin","Audi","Bentley","BMW","Bugatti","Buick","Cadillac","Chevrolet",
"Chrysler","Citroen","Dodge","Ferrari","Fiat","Ford","Geely","General Motors","GMC","Honda","Hyundai",
"Mercedes-Benz","Renault"]

carBrand = re.split("'|'.join(brands)|[^a-zA-Z ]+", carName)

print(carBrand)

But got this: ['Fiat Strada Working ', None, ' CS']

I need this: ['Fiat' , 'Strada Working 1.4 CS']

I'm having trouble finding the right regex to separate right after the join of brands.

1
  • If you split you loose the separator, so with this method Fiat will be gone Commented Jul 22, 2020 at 2:38

1 Answer 1

1

Your regex is completely wrong and you use the wrong re-method:

import re

carName = "Fiat Strada Working 1.4 CS"

brands = ["Acura","Alfa Romeo","Aston Martin","Audi","Bentley","BMW","Bugatti","Buick","Cadillac","Chevrolet",
"Chrysler","Citroen","Dodge","Ferrari","Fiat","Ford","Geely","General Motors","GMC","Honda","Hyundai",
"Mercedes-Benz","Renault"]

carBrand = list( re.match(f"({'|'.join(brands)})(.*)", carName).groups() )

print(carBrand)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that solved. Using list with re.match you can create separate elements in the list by applying regex in each one. So you create a group for the brands regex, and other for everything that comes after with ( . * )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.