Question

How do I get the year (4 digits) when given a source code, I can only detect the day (29), but could not detect the year(1997). There is something wrong in my regexp checking.

age = regexp(CharData,'(\d{1,4})','match','once')

For example,

Registered On March 29, 1997

Desired output: 1997

Error output: 29

for i = 1:2

   data2=fopen(strcat('DATA\PRE-PROCESS_DATA\F22_TR\f22_TR_pdata_',int2str(i),''),'r')
   CharData = fread(data2, '*char');  %read text file and store data in CharData
   fclose(data2);

   age = regexp(CharData,'(\d{4})','match','once')

end

file : f22_TR_pdata_1 --> Registered On June 24, 1997

file : f22_TR_pdata_2 --> Registered On March 29, 1997

Age: 1997

Was it helpful?

Solution

To only grab four digits

age = regexp(CharData,'(\d{4})','match','once')

Doing d{1,4} means look for numbers with a length between 1 and 4. Meaning, 1, 29, 123, 4444 would all match because their length is between 1 and 4

d{4} says, get me the number with exact length of 4. Meaning, 1997, 2001, 1800 would all match.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top