py regex

Jun 15, 2013 • Martin Schilling
  1. Which of the following functions would you use for replacing a certain pattern?

a) re.sub()
b) re.match()
c) re.findall()
d) re.search()

  1. You have a list of strings:
    a = [‘FLFL04′, ‘FLFL15′, ‘WWA02′, ‘WWA30′, ‘HSPQ221′]
    and you want to extract only letters without numbers for each string in the list. Which one is correct?

a) [re.search(‘([A-Z]+)’, val) for val in a]
b) [re.findall(‘([A-Z]+)’, val) for val in a]
c) [re.findall(‘[A-Z]’, val) for val in a]
d) [re.findall(‘([A-Z]+[a-z]+)’, val) for val in a]
e) np.unique(np.array([re.findall(‘([A-Z]+)’, val) for val in a]))