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:
-
Can’t select whole line of targets and select piece of target line.
(sometimes maybe length of numbers are not same) -
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 string20Match literally(?:Non capture group for the alternatives2[0-3]Match 20, 21, 22, 23|Or[01]\dMatch 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 .*