Search code examples
stringmatlabprefix

Matlab: How to delete prefix from strings


Problem: From TrajCompact, i find all the prefix and the value after prefix, using regexp, with this code:

[digits{1:2}] = ndgrid(0:4);
  for k=1:25
  matches(:,k)=regexp(TrajCompact(:,1),sprintf('%d%d.*',digits{1}(k),digits{2}(k)),'match','once');
  end

I want only the postfix of matches, how can delete the prefix from matches?


Solution

  • Method using regular expressions

    You can put the .* section in a group by enclosing it in parenthesis (i.e. (.*)). Matlab has some peculiar 'token' nomenclature for this. In any case, an example of how it works:

    [match, group] = regexp('25blah',sprintf('%d%d(.*)',2,5),'match','once','tokens');
    

    Then:

    1. match would be a char array containing '25blah'
    2. group would be a 1x1 cell array containing the string 'blah'.

    That is, the variable group would hold what you're looking for.

    Hack method

    Since your prefix is always two digits, you could also just take everything from the 3rd character of the match onwards:

    my_string = match(3:end);
    

    other comments

    You may want to require the prefix to occur at the beginning of the string by adding ^ to the beginning of your regular expression. Eg., make the line:

    [match, group] = regexp('25blah',sprintf('^%d%d(.*)',2,5),'match','once','tokens');
    

    As it is, your current regular expression would match strings like zzzzzzzzz25stuff. I'm not sure if you want that (assuming it can occur in your data).