Search code examples
matlaboctave

search in text file a parse line


I am using Octave, but since a Matlab answer might work too, I added both tags to this question. Here it is: I am trying to find this specific string in a text file :

</table>  
</td>

As you can see if you copy paste it, there is a parse line. I can't figure out how to search for it. Obviously this doesn't work:

separator_index = strfind(s,</table>
</td>);

I have tried to get rid of the spaces in the txt file using strtrim, but for some reason, the output file is exactly the same as the original one...

thanks for any help!


Solution

  • Octave

    Your string contains two spaces and a literal newline (i.e. ascii character number '10' in decimal, or '0a' in hexadecimal).

    You can spot this if you convert your string to double.

    To recreate that string, you need to use double quotes; compared to single quotes which do not 'interpret' escape sequences, double quotes do. Note that this is exclusive to octave; matlab treats single vs double quotes very differently.

    Therefore this will work:

    strfind( s, "</table>  \n</td>" )
    

    but this will not:

    strfind( s, '</table>  \n</td>' )
    

    Matlab

    If you want a matlab compatible solution, you'll just have to play with escape sequences and see if they work or not. One way to guarantee the correct string is generated is to convert it to string from the numerical character sequence, i.e.

    s = char([ 60, 47, 116, 97, 98, 108, 101, 62, 32, 32, 10, 60, 47, 116, 100, 62 ]);
    

    and then use this to find your match.