Separated by string pattern

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

If you have strings like this, I want to get only strings with date patterns.

Is it possible to filter out only certain strings by pattern?

  of # example data.
a = [
'Vaasa Lama the Korean alphabet'
On Feb. 2 is the ' 2020.'
, '145692'
, 'This is a few years ago and on February 2, by 2020.'.
]

# desired results.
a = [
' On Feb. 2 is 2020.'
, 'This is a few years ago and on February 2, by 2020.'.
]

python

2022-09-20 11:18

1 Answers

It is possible with regular expression. Regular expressions are difficult, but it is useful to study a little.

# Data Example
a = [
'Vaasa Lama the Korean alphabet'
On Feb. 2 is the ' 2020.'
, '145692'
, 'This is a few years ago and on February 2, by 2020.'.
]

# desired results.
a_ = [
' On Feb. 2 is 2020.'
, 'This is a few years ago and on February 2, by 2020.'.
]

import re

for s in a:
    res = re.search(r"\d{4} \d{1,2}month \d{1,2}day", s)
    if res:
        print(s)


a_filtered = [s for s in a re.search(r"\d{4} \d{1,2}month \d{1,2}day,s)]        
print(a_filtered)


2022-09-20 11:18

If you have any answers or tips


© 2022 pinfo. All rights reserved.