How can I extract multiple values after certain term with regexp?

조회 수: 4 (최근 30일)
Hi, I am new to using regexp and was wondering how I can extract all 'channel' values in my txt file. A snippet of the file is as follows:
Set 1
Average: 0.976
Channels: 0.973 0.985 0.988 0.989 0.981 0.986 0.99 0.977 0.953 0.992 0.979 0.923
Set 2
Average: 0.983
Channels: 0.978 0.98 0.983 0.985 0.976 0.982 0.985 0.984 0.994 0.991 0.991 0.969
I've found that using the expression '(?<=Channels\D*)\d*\.?\d+\' obtains only the first value after the word channels, so how can I obtain all 12 values for each set of channels?
Thank you kindly in advance!

채택된 답변

Guillaume
Guillaume 2016년 3월 7일
There are many ways to write a regular expression (see Walter's answer), depending on exactly what restriction you want to place on what is matched and not matched.
A simple way to modify your regular expression is simply to repeat the match pattern until the end of the line:
channelvalues = regexp(filecontent, '(?<=Channels\D*)(\d*\.?\d+(?:\s+|$))+', 'match');
channelvalues = cellfun(@str2num, channelvalues, 'UniformOutput', false)
%if all lines are guaranteed to have the same number of elements:
channelvalues = cell2mat(channelvalues')
  댓글 수: 1
Dhani Dharmaprani
Dhani Dharmaprani 2016년 3월 7일
편집: Dhani Dharmaprani 2016년 3월 7일
Thank you so much, this works perfectly! And thank you kindly for your explanation, it makes much more sense to me now!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2016년 3월 7일
'(?<=Channels:\s+).+$'
together with the parameter 'dotexceptnewline'

Stephen23
Stephen23 2016년 3월 7일
편집: Stephen23 2016년 3월 7일
As an alternative to regexp, you can simply read the file data directly using textscan:
fmt = ['Set%f\nAverage:%f\nChannels:',repmat('%f',1,12)];
fid = fopen('test.txt','rt');
M = cell2mat(textscan(fid,fmt));
fclose(fid);
It successfully reads all of the numeric data into one numeric matrix, much quicker than any solution using regexp would. I tested this code using your sample lines, saved into this file:
  댓글 수: 1
Dhani Dharmaprani
Dhani Dharmaprani 2016년 3월 7일
Thank you so much for this alternative suggestion! I will definitely try this!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by