plate number for a form in an app from a line of text in python

47 Views Asked by At

from a string of text, for example: ['ANT', 'ECUADOR', 'PCP-5453', '0184947', 'ANTi4LTe'] I need to extract only texts that are similar to PCP-5453. this should be:

3 letters one symbol '-' and from 3 to 4 numbers

Im doing it on python, for an app in Anvil.

is there a simple way of doing it?

1

There are 1 best solutions below

0
gezbleak On

Simple solution would be regex:

import re

arr = ['ANT', 'ECUADOR', 'PCP-5453', '0184947', 'ANTi4LTe']
resArr = []

for s in arr:
    if re.search("[a-zA-Z]{3}-[0-9]{3,4}", s):
        resArr.append(s)
print(resArr) #['PCP-5453']

The re.search() method checks if a given string matches the regex you give as an input. You can see an explanation of the regex here: https://regexr.com/6slpb