Regexp help for seperating numbers, matrices, strings
Show older comments
Hi all,
I have been trying to seperate strings of arbitrary fields including numbers, matricies, and more strings. These are comma seperated lists, and each list seperated by a semicolon.
samplestr = '{ 'auto', 1 ,1; 1, 'load('var.mat')', 'rand(nodes,1)' }'
Some of my strings also contain other strings, such as load.
I had regular expressions that original worked to seperate the comma seperated list from each other using
%Identifies arrays and matrices
matexp = '\[.*?(?=\])\]';
%Identifies strings and strings in parenthases within strings (i.e. 'load('var.mat')' is found as one string)
strexp = '''.*?(?(?='')(?<!\()))''';
%Identifies numbers
numexp = '(?:+|-)?\d*\.?\d*';
%numexp = '[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?';
%Identifies rows containing comma seperated list of (matexp|strexp|numexp)
%ending with by ';'
rowexp = ['((\s*(?:(?:' matexp ')|(?:' strexp ')|(?:' numexp '))\s*,?)*(?=(?:;|$))'];
rowgroups = regexp(samplestr,rowexp,'match')
rowgroups = ' 'auto', 1 ,1' ' 1, 'load('var.mat')', 'rand(nodes,1)' '
This will seperate the strings when all of the numbers do not include any exponents ( finds '1' not '1e-6'). The commented out 'numexp' can identify all numbers even with exponentials, but will not seperate the strings at the ';' anymore.
Does anyone know why this is and a fix? I have spent a lot of time trying to debug this but am not sure why it is happening, if anyone has a better way of doing it then please let me know.
3 Comments
Fangjun Jiang
on 14 Oct 2011
Why do you need to do this? I am interested in your use case. Are you trying to parse user input?
Joseph
on 14 Oct 2011
Joseph
on 14 Oct 2011
Answers (0)
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!