1

Considering a list of tickers as the following below:

tickers = ['EWZ US 05/29/20 C31','HSI US 12/30/20 C24900', 'SKG1C 24', 'S H1P 49']

How I can apply a filter to select only tickers that have a date? I'm try the following:

import re
tickers_off = [re.search(r'(\d+/\d+/\d+)', x) for x in tickers]

output expected:

['EWZ US 05/29/20 C31','HSI US 12/30/20 C24900']
Costa.Gustavo
  • 849
  • 10
  • 21

1 Answers1

1

You may use your current regex to filter the list items:

import re
tickers = ['EWZ US 05/29/20 C31','HSI US 12/30/20 C24900', 'SKG1C 24', 'S H1P 49']
rx = re.compile(r'\d+/\d+/\d+')
print( list(filter(rx.search, tickers)) )
# => ['EWZ US 05/29/20 C31', 'HSI US 12/30/20 C24900']

See the Python demo.

You might want to have a bit more precise date pattern, say, one from Regex to match Date or similar:

r'\b(?:0?[1-9]|1[0-2])/(?:0?[1-9]|[12]\d|3[01])/(?:19|20)?\d{2}\b'

See the regex demo.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563