I just started to learn regex. I created a regex for my project username where I want:
2 digits of day
2 digits of month
last 2 digits of current year
finally last 2 digits of the year after 4 years
ie I need to match output something like that:
23071923
where:
23 is the current day(dd)
07 is the month (mm)
19 is current year(YY)
23 is the year after 4 years(yy)
But I am confused how will it match the current year and only take last 2 digits of the year. and how i can match the end of regex is correct and is 4 added in the year or not?
You could use the trick with replacing the input string with modified string, which does math:
var x = "23071923";
var x2 = Regex.Replace(x, @"^(\d{2})(\d{2})(\d{2})(\d{2})$", m =>
m.Groups[1].Value + m.Groups[2].Value + m.Groups[3].Value +
(int.Parse(m.Groups[3].Value) + 4));
bool is_match = x == x2;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.