MATLAB Answers


how to use regexp to get the last characters of a line

Asked by Lucy Cathwell on 21 Aug 2019
Latest activity Edited by per isakson
on 13 Sep 2019 at 7:50
file = uigetfile('*.txt','Select the text file to parse');
expr='[^\n]*Machine Name=[^\n]*';
matches=regexp(text, expr,'match');
I have a really long complicated text file that I want to parse. It is really long and is a list of lines. There are no comma delimiters, but the machine name that I am looking for is always at the end of the line ... like
121212323 : Machine Name = roboDog
121222323 : Machine IOS = Android
The code above was my attempt to desplay the lines which contained the keyword. It wouldn't work, but I would ideally like each Machine Name to be stored in a vector like machineName={'roboDog' ; 'roboCat'}


Sign in to comment.

1 Answer

Answer by the cyclist
on 21 Aug 2019
Edited by the cyclist
on 21 Aug 2019

Your definition of expr does not have a space between "Machine Name" and the equals sign, but your text example does. When I put that space in, it found the match.


matches =
0×0 empty cell array
Did this happen for you too? there are definitely plenty of matches
With the attached text file, and this code:
expr='(?<=Name = )\w*';
matches=regexp(text, expr,'match')
I get a 1x2 cell array:
matches =
1×2 cell array
{'roboDog'} {'roboCat'}
I suspect that machine names might not strictly match \w as that does not include '-' for example. I suggest
expr = '(?<=Name =\s*).*?(?=\s*)$';
matches = regexp(text, expr, 'match', 'lineanchors', 'dotexceptnewline')
This strips out leading and trailing whitespace and accepts anything between

Sign in to comment.