Find string of relative directory with regexp

조회 수: 2 (최근 30일)
Bob Thompson
Bob Thompson 2022년 2월 21일
댓글: Bob Thompson 2022년 2월 21일
I have a string in a file that I want to find and replace. The string contains a directory that is defined relative to another location variable and therefore contains '..\' type callings in the name. How can I find something like this with regexp? I am able to locate it fine with strfind, so I haven't misspelled anything, but regexp can't find it. I assume this is because something is being treated as an escape character, but how can I get around that?
txt = 'myfile = ''..\..\pwd''; myotherfile = ''..\..\pwd'';';
txtfind = strfind(txt,'myfile = ''..\..\pwd'';')
txtrerf = regexp(txt,'myfile = ''..\..\pwd'';','match')
txtfind =
1
txtrerf =
0 x 0 empty cell array

채택된 답변

David Hill
David Hill 2022년 2월 21일
txtrerf = regexp(txt,'myfile = ''[.]{2}\\{1}[.]{2}\\{1}pwd'';','match')
  댓글 수: 1
Bob Thompson
Bob Thompson 2022년 2월 21일
On further experimentation, the {1} for the slashes is unnecessary.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by