Extract value until regexp case match

Hi,
I have this string. I was hoping to use regexp in order to extract the time and date embedded in this string. I was going to use regexp '.*?<=216', as prior to 216 is all the values I was hoping to extract. I was hoping that there would be an easy way to extract the date and time to a separate cell array. Thanks for any and all help.
"9/12/2022 7:38:51 PM
216 Indicator"

2 件のコメント

Rik
Rik 2022 年 9 月 23 日
While this could work, is there a reason why you can't match the date itself directly?
Dyel
Dyel 2022 年 9 月 23 日
Is there a way using regexp to extract the datetime value, or something similar from a char array?

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2022 年 9 月 23 日

1 投票

Does the data you want to match always occur at the start of the string?
A = "9/12/2022 7:38:51 PM 216 Indicator";
B = regexp(A,'^\S+\s+\S+\s[AP]M','match','once')
B = "9/12/2022 7:38:51 PM"
T = datetime(B,'inputFormat','d/M/u h:m:s aa')
T = datetime
09-Dec-2022 19:38:51

1 件のコメント

Dyel
Dyel 2022 年 9 月 26 日
This did the trick. Thanks Stephen.
How I did it was to split it up using the SplitString fuction, and then convert the string with the date to datetime, in case anyone may find it useful.
for j = 1:length(cf)
loopname = cf(j).name;
filetext = fileread(loopname);
txtStr = convertCharsToStrings(filetext);
indVal{i,j} = cell2mat(regexp(filetext, '(?<=Indicator[^0-9]*)[+-]?[0-9]*\.?[0-9]+', 'match'));
splitStr = regexp(txtStr,'\n','split');
dateStr = splitStr{1};
date = datetime(dateStr);
dateVal{i,j} = date;
end

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2022 年 9 月 23 日

コメント済み:

2022 年 9 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by