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
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.
S H
S H 2019년 5월 14일
편집: S H 2019년 5월 14일
I want to learn how to match complex strings such as
v0(2:k-1,10)
using regexprep. In my question posted here, I want to learn how to change
'v0(2:k-1,10)'
with another string such as
'0'
Adam Danz
Adam Danz 2019년 5월 14일
편집: Adam Danz 2019년 5월 14일
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)'
S H
S H 2019년 5월 14일
편집: S H 2019년 5월 14일
The question marks ? can change in the string and can have different length and patterns:
'v0(??????,10)'
The first unchanging part of the string I want to change is
v0(
and the last unchanging part of the string is
,10)
Adam Danz
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
S H 2019년 5월 14일
I tested your answer on my lengthy strings and it works as expected. Thank you for teaching me the use of [^,]* in regexprep. I was reading Matlab regexp help page a few times and such an important and helpful combination as [^,]* is not clearly explained there.
Adam Danz
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

Adam Danz
Adam Danz 2019년 5월 14일
편집: Adam Danz 2019년 5월 14일

0 개 추천

Here you are.
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 = 'v0\([^,]+,10\)';
replace = '0';
out = regexprep(str, expression, replace)
Compare input/output (I added space in output for comparison)
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)) %input
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)) %output

댓글 수: 2

S H
S H 2019년 5월 14일
It works nicely. Thank you very much.
Adam Danz
Adam Danz 2019년 5월 14일
편집: Adam Danz 2019년 5월 14일
Great! Just so you know...
  • v0\( Start the expression at v0(
  • [^,]+ Stop matching just before the next comma
  • ,10\) make sure the expression ends with ,10)
A nice workstation to develop regular expressions: https://regex101.com/

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

S H
2019년 5월 14일

댓글:

2019년 5월 14일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by