Legacy implementation of strtrim prior to R2016a
2 ビュー (過去 30 日間)
古いコメントを表示
It looks like strtrim changed in R2016a to not trim significant whitespace characters. However, I have a need for a legacy implementation. Is it possible to provide one that works in R2016a, but also trims significant whitespace characters?
0 件のコメント
採用された回答
Guillaume
2016 年 8 月 30 日
regexp(yourstring, '(?<=^\W*)\w.*?(?=\W*$)', 'match', 'once')
should do the trick
4 件のコメント
Guillaume
2016 年 8 月 31 日
編集済み: Guillaume
2016 年 8 月 31 日
First of all, I made a mistake in the regex, it should have been \s instead of \W and \S instead of \w (yes upper and lower case should be swapped as well), so:
regexp(yourstring, '(?<=^\s*)\S.*?(?=\s*$)', 'match', 'once')
This will not speed up the regex, the slow down is probably caused by the non-greedy all capturing star .*? followed by the greedy capturing \s*, which causes a lot of backtracking.
However, this regex will do the same and should be significantly faster:
regexp(yourstring, '(?<=^\s*)\S.*\S(?=\s*$)', 'match', 'once')
As for a legacy option for strtrim, this is something you would have to ask Mathworks through a service request.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!