Home > OS >  Regular expression that filters data
Regular expression that filters data

Time:08-29

I am thinking of making a filter with python that allows me to search the information of a file by giving me a regular expression, the search must be "year-month".

I have a file with the following fields

date vehicle license plate number number of people in the accident comments

and a txt file with the following information corresponding to the above fields:

2022-12-01;ABC-101;1;nothing
2022-12-02;ABC-101;1;nothing
2022-12-03;ABC-101;1;nothing
2022-12-04;ABC-102;1;nothing
2021-10-07;ABC-103;1;nothing
2021-10-08;ABC-103;1;nothing
2021-10-09;ABC-104;1;nothing
2021-10-10;ABC-105;1;nothing

Which regular expression allows me to select all the information of that accident by year-month?


for example

  • input: 2022-12
  • output:
2022-12-01;ABC-101;1;nothing
2022-12-02;ABC-101;1;nothing
2022-12-03;ABC-101;1;nothing
2022-12-04;ABC-102;1;nothing

  • input: 10-2021
  • output:
2021-10-07;ABC-103;1;nothing
2021-10-08;ABC-103;1;nothing
2021-10-09;ABC-104;1;nothing
2021-10-10;ABC-105;1;nothing

CodePudding user response:

I just tested this and this works as a pattern matching.

    import re
    
    s = '2022-12-01;ABC-101;1;nothing'
    
    result = re.match('^\d{4}-\d{2}', s)

^ matches for the starting position
\d matches a digit (or numeric figure)
{n} matches n patterns to the left of it so \d{4} matches four digits

CodePudding user response:

This identifies months from 01 to 12.

    s = '2022-12-01;ABC-101;1;nothing'
    
    result = re.match('^\d{4}-(01|02|03|04|05|06|07|08|09|10|11|12)', s)

The | character acts as a logical or

  • Related