Find string of relative directory with regexp

3 ビュー (過去 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 Center および File ExchangeFile Operations についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by