complex string match with regexprep
古いコメントを表示
What should be the expression in the following script to generate out from str?
str='diff([v0(k-1,2)-v0(k-1,1) v(2)-v(1)])/diff(t0(k-1:k))+((v(2)-0)*diff(t0(k-1:k))+sum((v0(2:k-1,2)-v0(2:k-1,10)).*diff(t0(1:k-1)).'',1))';
expression = ???;
replace = '0';
out=regexprep(str,expression,replace);
out='diff([v0(k-1,2)-v0(k-1,1) v(2)-v(1)])/diff(t0(k-1:k))+((v(2)-0)*diff(t0(k-1:k))+sum((v0(2:k-1,2)-0).*diff(t0(1:k-1)).'',1))'
7 件のコメント
Adam Danz
2019 年 5 月 14 日
It would be helpful for you to explicitly show us the the differences between str and out so we can be sure there weren't typos and so we can be certain of the pattern you want to extract.
For the string 'v0(2:k-1,10))' what parts of it might vary and what parts will always be the same. For example, will it always have any of these structures?
- v0(#:k-#,#)
- v0(#:_-_,#)
- v0(#:_,#)
- v0(#:_,_)
- v0(_:_,_)
- _(_:_,_)
- _(_,_)
The more specific (higher up on my list), the better.
I'm assuming the string won't always be identical to 'v0(2:k-1,10)'
Adam Danz
2019 年 5 月 14 日
Got it. Thoroughly test my answer and if there are any problems, you can leave a comment under my answer and we can continue the discussion.
S H
2019 年 5 月 14 日
Adam Danz
2019 年 5 月 14 日
There are so many options with regular expressions that it's hard to capture them all in one document. I usually just google awkward phrases like "regular expressions match any character until" to remind myself of the options. The website I suggested in my answer is another great tool.
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!