0

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?

  • What you have tried so far? “Show/tell me how to solve this coding problem” is not a Stack Overflow issue. We expect you to make an honest attempt, and then ask a specific question about your algorithm or technique. Stack Overflow is not intended to replace existing documentation and tutorials. Python string methods are described here: https://docs.python.org/3/library/stdtypes.html#string-methods – AlexK Aug 25 '22 at 19:42

1 Answers1

1

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

gezbleak
  • 36
  • 1
  • 4