How to get year format number from a string?

Hi,

I have a string from a Word Document
phprlCapUPM

I want to compare the year from the word document and the year from our computer.
I have already get the year from the computer using System.DateTime.Now.ToString(“yyyy”)

The problem now is how do I get the year from the string, shown in the picture above?
The string has 2 number, 2017 and 2.
How do I know which number is the year?

Thank you

1 Like

Hi @Ying_Zhen_Lee,

First you can extract the year from string and compare it with System.DateTime.Now.ToString(“yyyy”).

Please assign string_var=System.Text.RegularExpressions.Regex.Match(your_string,"(\d{4})(?=\sTime)").ToString.Trim

Warm regards,
Nimin

2 Likes

use CINT(convert.ToDateTime([your date]).ToString(“yyyy”)) to compare with your numeric year

4 Likes

Hi @nimin,

Thanks for your reply!

It works :smile: However may I ask (?=\sTime) is it for DateTime format ?

Thank you

1 Like

Hi @Sheshrocks,

Thanks for your reply and help! :smile:

2 Likes

Hi @Ying_Zhen_Lee,

This is a look ahead regular expression. For the string, “Feb 2017 Time allowed: 2 hrs” only “\d{4}” is enough to capture the year. But if there is a possibility of more than one year in your string, for eg: “Feb 2017 Time allowed: 2 hrs. Now it’s 2019” , (?=\sTime) will looks for presence of 4 digits(\d{4}) before the word “Time”. That’s it. :grinning:

Regards,
Nimin

1 Like

Hi @nimin,

Thanks for the explanation, now I get it! :smile:

Thank you

2 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.