How to search and replace a text from a character vector?

조회 수: 2 (최근 30일)
Kazi Alam
Kazi Alam 2021년 7월 8일
댓글: Kazi Alam 2021년 7월 8일
Hallo, Thanks for reading,
I have a character array (1x22222) read from a text file. Using the character vector I would like
to serach for the following line within the array: ( ' * ' here is represented as random text part)
'(:PAR :N CORNERS :DIM (4 2) :V #2A(*)'
After that I would like to replace the text with following line:
'(:PAR :N CORNERS :DIM (4 2) :V #2A((0.0 4.0) (2.5 4.0) (2.5 0.0) (0.0 0.0))'
I have the following lines of code but it cannot find the intended text
f = regexprep(character_array,'(:PAR :N CORNERS :DIM (4 2) :V #2A(*)'],...
'(:PAR :N CORNERS :DIM (4 2) :V #2A((0.0 4.0) (2.5 4.0) (2.5 0.0) (0.0 0.0))');

채택된 답변

Bhavya Chopra
Bhavya Chopra 2021년 7월 8일
Please note that ‘(‘ is a special character. For replacing the described lines with the intended text, please replace the expression (second argument of the regexprep function) with:
'\(:PAR :N CORNERS :DIM \(4 2\) :V #2A\(.*?\)'
  댓글 수: 1
Kazi Alam
Kazi Alam 2021년 7월 8일
@Bhavya Chopra thank you very much. A follow-up question, if you are interested.
I have similar lines with varying number as you can see in photo below ( numbered the lines). If I want to change the numbers on individual lines what would be the procedure. Right now with wildcard only the first line can be changed.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by