Take the 2-minute tour ×

Hi need help in using regexp for condition matching.

ex.my file has the following content {hello.program='function'`; bye.program='script'; }

I am trying to use regexp to match the string that has .program='function' in them:

pattern = '[.program]+\=(function)'

also tried pattern='[^\n]*(.hello=function)[^\n]*';

pattern_match = regexp(myfilename,pattern , 'match')

but this returns me pattern_match={} while i expect the result to be hello.program='function'`;

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

If 'function' comes with string-markers, you need to include these in the match. Also, you need to escape the dot (otherwise, it's considered "any character"). [.program]+ looks for one or several letters contained in the square brackets - but you can just look for program instead. Also, you don't need to escape the =-sign (which is probably what messed up the match).

cst = {'hello.program=''function''';'bye.program=''script'''; };
pat = 'hello\.program=''function''';
out = regexp(cst,pat,'match');
out{1}{1} %# first string from list, first match


In response to the comment

my file contains

m2 = S.Parameter;
m2.Value = matlabstandard;
m2.Volatility = 'Tunable';
m2.Configurability = 'None';
m2.ReferenceInterfaceFile ='';
m2.DataType = 'auto';

my objective is to find all the lines that match, .DataType='auto'

Here's how you find the matching lines with regexp

%# read the file with textscan into a variable txt
fid = fopen('myFile.m');
txt = textscan(fid,'%s');
txt = txt{1};

%# find the match. Allow spaces and equal signs between DataType and 'auto'
match = regexp(txt,'\.DataType[ =]+''auto''','match')

%# match is not empty only if a match was found. Identify the non-empty match
matchedLine = find(~cellfun(@isempty,match));
share|improve this answer
Thanks!but i have to run this on a number of files, and i do not know the exact number of strings they will have.so i cant define them like cst has been. all i know that it should match program=function. i tried using pat = 'hello\.program=''function''';and it returned null:(.any further advice? –  learningMatlab Dec 13 '12 at 17:13
@learningMatlab: I created cst just to have something to test - I assumed that your file is read into a cell array of strings. Also, do you need to match program='function', or program=function? Or is 'function' any arbitrary function name? –  Jonas Dec 13 '12 at 17:22
function is an arbitrary name. and i need to match .program='function' –  learningMatlab Dec 13 '12 at 17:24
@learningMatlab: say your function names are something and otherThing. Would your code look like hello.program=something or hello.program='something'? Could there be spaces around the equal sign? –  Jonas Dec 13 '12 at 17:54
@learningMatlab: Can you paste the actual line from the file that you want to be parsed by regexp? –  Jonas Dec 13 '12 at 18:13
show 2 more comments

Try this as it matches .program='function' exactly:


I think this did not work:


because of how the []'s work. Here is a link explaining why I say that: http://www.regular-expressions.info/charclass.html

share|improve this answer
Thannks. (\.)program='function' returns null :( –  learningMatlab Dec 13 '12 at 17:23
I tested this at regextester.com and it worked for me successfully there. Are you running this on a command-line or, perhaps, within an application like BASH or PHP? Would you mind sharing your exact command with us? –  jkovba Dec 13 '12 at 17:44
I am using it in MATLAB –  learningMatlab Dec 13 '12 at 17:49
Have you tried this: (\.)program='[a-zA-Z0-9]+' –  jkovba Dec 13 '12 at 18:00
...if not, here's all I've got left without being able to test in MATLAB myself: (?:\.)program='[a-zA-Z0-9]+' –  jkovba Dec 13 '12 at 18:04
add comment

Your Answer


