No BSD License  

Highlights from
ExpandRegexSet

image thumbnail
from ExpandRegexSet by Michael Robbins
Turns an expression like abc[def] into {'abcd', ...

new=ExpandRegexSet(in_exp)
function new=ExpandRegexSet(in_exp)
% EXPANDREGEXSET is a function written for CSSM as an
% example and is not well thought out.
% It turns an expression like abc[def] into {'abcd', ...
% 'abce','abcf'}
%
% This is similar to REGEXPIOR which can be found in my REGEXTOOLS
% (regular expression tools) at MATLAB Central
%
% USAGE
% >> ExpandRegexSet('abc[def]ghi[jkl][mno]')'
% ans = 
% Columns 1 through 6
% 'abcdghijm' 'abcdghijn' 'abcdghijo' 'abcdghikm' 'abcdghikn' 'abcdghiko'
% Columns 7 through 12
% 'abcdghilm' 'abcdghiln' 'abcdghilo' 'abceghijm' 'abceghijn' 'abceghijo'
% Columns 13 through 18
% 'abceghikm' 'abceghikn' 'abceghiko' 'abceghilm' 'abceghiln' 'abceghilo'
% Columns 19 through 24
% 'abcfghijm' 'abcfghijn' 'abcfghijo' 'abcfghikm' 'abcfghikn' 'abcfghiko'
% Columns 25 through 27
% 'abcfghilm' 'abcfghiln' 'abcfghilo'
%
% CAVEAT:
%   Nested brackets and escaped brackets are not supported.
%   Only square brackets are supported
%
% IT'S NOT FANCY BUT IT WORKS

% Michael Robbins
% robbins@bloomberg.net
% michael robbins@us.cibc.com

nargin=0;
if nargin<1
    in_exp='abc[def]ghi[jkl][mno]';
end;
left_brac=find(in_exp=='[');
right_brac=find(in_exp==']');
% ARE THERE BRACKETS?
if isempty(left_brac)|isempty(right_brac)
    error('No paired brackets')
end;
% CHECK THAT BRACKETS MATCH
if length(left_brac)~=length(right_brac)
    error('Brackets don''t match');
end;
% CHECK NESTING
% later
% TAKE CARE OF ESCAPED BRACKETS
% later

old={in_exp};
for i=1:length(left_brac)
    new=[];
    for j=1:length(old)
       temp=old{j};
       [b,e]=regexp(temp,'\[[^\]]*\]');
       chars=temp(b(1)+1:e(1)-1);
       temp=[temp(1:b(1)-1) '%c' temp(e(1)+1:end)];
       temp=cellstr(reshape( ...
          sprintf(temp,chars).', ...
          length(temp)-1, ...
          length(chars)).');
      if ~isempty(new)
          new={new{:} temp{:}};
      else
          new=temp;
      end;
   end; % for j
   old=new;
end; % for i
new=new';

Contact us