Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bookmark lines that start with a year and month

I have a list like following:

20110512192714
20794709157481
20121203175303
20794709157481
20210131201708
20211101050154
20220501022609
79776361952441

Now I want to select lines that start with a year and month from left side in notepad++ by regex.
Note that years must start from 2000 to 2023.
I tried following regex:

20(?:0[0-9]|1[0-9]|20)[0-1][0-9]

above regex working good but have two problem:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. Can’t select whole line of targets and select piece of target line.
    (sometimes maybe length of numbers are not same)

  2. It can’t select
    some lines like following lines:

    20211101050154
    20220501022609

where is my regex problem?

>Solution :

Your pattern 20(?:0[0-9]|1[0-9]|20)[0-1][0-9] matches:

20 followed by a range 00-09 or range 10-19 or 20 followed by either 00-19 or 01-19


If yo want to match a line that starts with 2000 – 2023 and should only contain digits:

^20(?:2[0-3]|[01]\d)\d*$

If you want to include matching a month from 1 – 12 with an optional leading zero:

^20(?:2[0-3]|[01]\d)(?:0?[1-9]|1[012])\d*$

The year pattern matches:

  • ^ Start of string
  • 20 Match literally
  • (?: Non capture group for the alternatives
    • 2[0-3] Match 20, 21, 22, 23
    • | Or
    • [01]\d Match from 00 to 19
  • ) Close the non capture group
  • \d* Match optional digits
  • $ End of string

See a regex demo.

If you want to match the rest of the line that could contain any character, you can change the last part of the pattern from \d*$ to .*

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading