Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Regular expression numpty

Subject: Regular expression numpty

From: Jane T

Date: 21 Aug, 2012 20:49:15

Message: 1 of 8

Hey,

I have several strings that look similar to:

#Calib[001]=22815

I would like to extract the number after the equals sign. After much struggle the best I can achieve is

cal = regexp(str,'=(\w+.*?)','match')

which returns

'=22586'

I just want the number and would really appreciate some help here.

Many thanks

Subject: Regular expression numpty

From: Barry Williams

Date: 22 Aug, 2012 10:09:14

Message: 2 of 8

"Jane T" wrote in message <k10s8b$pk8$1@newscl01ah.mathworks.com>...
> Hey,
>
> I have several strings that look similar to:
>
> #Calib[001]=22815
>
> I would like to extract the number after the equals sign. After much struggle the best I can achieve is
>
> cal = regexp(str,'=(\w+.*?)','match')
>
> which returns
>
> '=22586'
>
> I just want the number and would really appreciate some help here.
>
> Many thanks

This may be a bit ugly, but if no one else has a better reply, it works.

str='#Calib[001]=22815';
[tok rem]=strtok(str,'=');
num=str2num(rem(2:end));

Barry

Subject: Regular expression numpty

From: Bruno Luong

Date: 22 Aug, 2012 10:29:22

Message: 3 of 8

"Jane T" wrote in message <k10s8b$pk8$1@newscl01ah.mathworks.com>...
> Hey,
>
> I have several strings that look similar to:
>
> #Calib[001]=22815
>
> I would like to extract the number after the equals sign. After much struggle the best I can achieve is
>
> cal = regexp(str,'=(\w+.*?)','match')
>
> which returns
>
> '=22586'
>
> I just want the number and would really appreciate some help here.

% Look around operator
 cal = regexp(str,'(?<==)(\d+)','match')

% Bruno

Subject: Regular expression numpty

From: Jane T

Date: 23 Aug, 2012 16:47:07

Message: 4 of 8

Thanks to both of you for your replies.

Bruno, I don't quite understand your method so haven't been able to figure this out. Can it be extended to also capture negative numbers? And what's the best method of extending it to a cell array of strings to hopefully result in a matrix of numbers? Would I use cellfun?

Many thanks again

Subject: Regular expression numpty

From: Jane T

Date: 23 Aug, 2012 17:05:07

Message: 5 of 8

"Jane T" wrote in message <k15mqb$ma5$1@newscl01ah.mathworks.com>...
> Thanks to both of you for your replies.
>
> Bruno, I don't quite understand your method so haven't been able to figure this out. Can it be extended to also capture negative numbers? And what's the best method of extending it to a cell array of strings to hopefully result in a matrix of numbers? Would I use cellfun?
>
> Many thanks again

Some more details ...

I have a textfile containing lots of information. I have read the file into Matlab using

[filename, pathname] = uigetfile('*.gva');
% Read file contents to memory
C = fileread([pathname,f(file).name]);
% Delimit by carriage return / newline
C = regexp(C,'\r','split')';

I then search the file to find all the rows in C relating to the calibration data

temp = regexp(C, '\#Calib\[','start');
ind = find(~cellfun('isempty',temp));

each string in C(ind) is of the form '#Calib[248]=-3328'

I want to extract the numbers of the calibration waveform to perform some analysis on it. I've managed to extend Bruno's method to get all numbers using

cal = regexp(C(ind),'(?<==)(.\d+)','match');

I now have a cell array where each cell is a {1x1 cell} containing the number contained in that row as a string.

How do I these values to be numbers in a vector? Does my methodology so far seem reasonable? I'm still trying to improve and come more efficient with my coding, but have no one hear to discuss these things with.

Thanks again

Subject: Regular expression numpty

From: Bruno Luong

Date: 23 Aug, 2012 17:10:08

Message: 6 of 8

"Jane T" wrote in message <k15mqb$ma5$1@newscl01ah.mathworks.com>...
> Thanks to both of you for your replies.
>
> Bruno, I don't quite understand your method so haven't been able to figure this out. Can it be extended to also capture negative numbers? And what's the best method of extending it to a cell array of strings to hopefully result in a matrix of numbers? Would I use cellfun?

>> str={'abc=123','def=-456'}

str =

    'abc=123' 'def=-456'

>> c = regexp(str,'(?<==)(-*\d+)','match'); % check 'look around' in the doc of regular expression if you want to know exactly how it works

>> str2double([c{:}])

ans =

   123 -456

% Bruno

Subject: Regular expression numpty

From: Jane T

Date: 20 Sep, 2012 20:45:13

Message: 7 of 8

Thank you for your responses thus far. I do have another question though:

I think I've got to grips with look arounds and regular expressions. My question now is with regards to speed.

In the example above the first thing I did was to split the text file at the end of each line:

[filename, pathname] = uigetfile('*.gva');
% Read file contents to memory
C = fileread([pathname,f(file).name]);
% Delimit by carriage return / newline
C = regexp(C,'\r','split')';

This made finding the location of each of the variables I wanted 'clunky' eg

% Read the internal temperature of the instrument
rows = regexp(C, '#Temperature=','start');
ind = find(~cellfun('isempty',rows));
tempStr = regexp(C(ind),'(?<=#Temperature=*\s+)(-*\d+\.*\d*)','match');
temp(1,file) = str2double([tempStr{1}])';

I need to locate about 20 different variables, so I decided to keep the textfile intact and not split it at each newline (I removed the line: C = regexp(C,'\r','split')';)

Now I can find each variable in one line eg

% Read the internal temperature of the instrument
temp(1,file) = str2double(regexp(C,'(?<=#Temperature=\s*)([0-9.-]+)',...
    'match','once'));

My code is now cleaner and easier to read, but went from taking ~2sec to ~42sec. It's the same textfile, so I guess I'm confused by the logisitics behind the operation to make such a difference in time. Could anyone explain this to me please?

I guess I need to go back to delimiting the file before searching it. Would anyone be able to help me re-write the four lines of code above to make them less 'clunky' and easier to read?

Many thanks
Jane

Subject: Regular expression numpty

From: Jane T

Date: 24 Sep, 2012 15:39:09

Message: 8 of 8

Is anyone able to help me with this?

To summarise, I'm trying to understand why it is quicker to search my textfile after is has been delimited at line feeds, as opposed to keeping the file intact.

I would also like some help with searching the delimited textfile without the number of cells getting out of hand.

Many thanks

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us