function [FA, NUC, RA] = matchReads(Matches,FR, FB, RB, LIB, kind)

% create regular expression that will be used to search for reads    
expres = MakeExpression(FB, RB, LIB, kind);

% matching in done in chunks to avoid overloading the memory during the
% regexp search. This chunk is controlled only locally. It might not be 
% necessary in the future, where entire aligned2unique function is written
% as chunky-function that reads and processes input in chunks.
CHUNK=100000;

% fuse all nucleotides into one long string; insert an extra space %%%%%%%%
    cMatches={};
    if size(Matches,1)>CHUNK
        for ii=1 : floor( size(Matches,1)/CHUNK )
            cMatches{ii}=Matches( CHUNK*(ii-1)+1:CHUNK*ii );
        end
        cMatches{ii+1}=Matches( CHUNK*ii+1:end );
    else
        cMatches{1}=Matches;
    end
    
%%%%%%%%%%%%%%%%%%%% loop through sub-Matches to find the library %%%%%%%%%
%%%%%%%%%%%%%%%%%%%% using regexp constructed above %%%%%%%%%%%%%%%%%%%%%%%

    for ii=1:numel(cMatches)

        SP = char(32*ones(size(cMatches{ii},1),1));
        input = sprintf('%s\r\n', [FR(cMatches{ii},:) SP]');

        % search for the regular expression (the slowest step)
        temp = regexp(input,expres,'names');
        
        if ii==1
            out=temp;
        else
            out = [out temp];
        end   
        fprintf('.');
    end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    if isempty(out)
        disp(['; zero matched reads, ' num2str(toc,'%10.1f')   ' s'])
        NUC = '';
        FA  = '';
        RA  = '';
        return    % do not save anything if no reads have been matched
    end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    NUC = char(out.SEQ);
    FA  = char(out.FA);
    RA  = char(out.RA);
    
    fprintf([num2str(size(out,2)) ' mapped ' ...
                                 '(' num2str(toc,'%10.1f')   ' s); ']);
end
