Check the format of a string.

조회 수: 21 (최근 30일)
Govind Sankar Madhavan Pillai Ramachandran Nair
답변: VINAYAK LUHA 2024년 1월 10일
I have a string variable and I need to check if it is of the given format. Its actually a date time. There are two types.
One is '20240109_153535' and I need to confirm this is 'yyyyMMdd_HHmmss'.
And other is '2023-08-15T06:10:09.89' and I need to confirm this is 'yyyy-MM-dd''T''HH:mm:ss.SSS'
How do I do that? Thank you. Thank You.

채택된 답변

Hassaan
Hassaan 2024년 1월 10일
For 'yyyyMMdd_HHmmss' Format:
% Example date-time string
dateTimeStr1 = '20240109_153535';
% Regular expression pattern for 'yyyyMMdd_HHmmss'
pattern1 = '^\d{4}(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})$';
% Check if the string matches the pattern
if ~isempty(regexp(dateTimeStr1, pattern1, 'once'))
disp('The string is in the correct "yyyyMMdd_HHmmss" format.');
else
disp('The string is NOT in the correct "yyyyMMdd_HHmmss" format.');
end
The string is in the correct "yyyyMMdd_HHmmss" format.
For 'yyyy-MM-dd''T''HH:mm:ss.SSS' Format:
% Example date-time string
dateTimeStr2 = '2023-08-15T06:10:09.89';
% Regular expression pattern for 'yyyy-MM-dd''T''HH:mm:ss.SSS'
% Note: Since 'SSS' means milliseconds and your example has only two digits, I'll use 'SS' in the pattern.
pattern2 = '^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{2}$';
% Check if the string matches the pattern
if ~isempty(regexp(dateTimeStr2, pattern2, 'once'))
disp('The string is in the correct "yyyy-MM-dd''T''HH:mm:ss.SS" format.');
else
disp('The string is NOT in the correct "yyyy-MM-dd''T''HH:mm:ss.SS" format.');
end
The string is in the correct "yyyy-MM-dd'T'HH:mm:ss.SS" format.
  • The caret (^) at the beginning of the pattern denotes the start of the string.
  • \d{4} matches exactly four digits, \d{2} matches exactly two digits, and so on.
  • The dollar sign ($) at the end of the pattern denotes the end of the string.
  • regexp function is used with the 'once' option to return a match if the string conforms to the pattern.
  • For the milliseconds part, I used \d{2} based on your example, but if you need exactly three digits for milliseconds, you should use \d{3} in your pattern instead.
Remember that this method only checks if the string conforms to the specified format. It does not validate whether the date and time themselves are correct (e.g., February 30th would be considered valid by the regex). To perform an actual date-time validation, you would need to try converting the string to a date-time object using datetime and catch any errors that occur for invalid dates.
---------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

추가 답변 (1개)

VINAYAK LUHA
VINAYAK LUHA 2024년 1월 10일
Hi Govind,
Apparently, you have datetime of different formats and want to assert that a particular datetime is in a particular format. Besides having the option to do regex checking on the string converted datetime, you can also leverage the "try" block to try to obtain a particular datetime in a particular format and asset accordingly.
Refer to the below code snippet for an example.
str1 = '20240109_153535';
str2 = '2023-08-15T06:10:09.89';
format = assertDateTimeFormat(str1)
format = assertDateTimeFormat(str2)
function df = assertDateTimeFormat(str)
f1 = 'yyyyMMdd_HHmmss';
f2 = 'yyyy-MM-dd''T''HH:mm:ss.SSS';
try
datetime(str, 'InputFormat', f1);
df ="in yyyyMMdd_HHmmss Format";
return
end
try
datetime(str, 'InputFormat', f2);
df='in yyyy-MM-dd''T''HH:mm:ss.SSS';
return
end
end
Hope this answers your query.
Regards,
Vinayak Luha

카테고리

Help CenterFile Exchange에서 Time Series Objects에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by