O. Watson O. Watson - 1 month ago 11
Python Question

Finding string in the format of TEXT-###, e.g SFS-444 inside of another string

Pretty much as the statement states, I'm trying to figure out how to find text that follows this format TEXT-### inside of another string. However, there may be a lot of words or multiple numbers, such as,

FRS-44215
SLMP-44
AG-1


So for example I have this text.

"Lorem ipsum dolor sit amet, adversarium suscipiantur
has ea, duo at alia assum, eu ius hinc
aliquip percipitur SGF-7852 Nec ne
nisl duis volutpat"


The code would pick out
SGF-7852

Answer

Use a regular expression to define the pattern that you are looking for, and then search the string for it.

>>> s = '''"Lorem ipsum dolor sit amet, adversarium suscipiantur 
... has ea, duo at alia assum, eu ius hinc 
... aliquip percipitur SGF-7852 Nec ne 
... nisl duis volutpat"'''
>>> e = r'[A-Z]+-\d+'
>>> import re
>>> re.findall(e, s)
['SGF-7852']

Here the pattern is:

  • [A-Z]+ (one or more capital letters)
  • - followed by a dash
  • \d+ followed by one or more numbers