フィルターのクリア

How to correct unexpected behavior of `regexprep`?

1 回表示 (過去 30 日間)
Felipe Jiménez Hernández
Felipe Jiménez Hernández 2020 年 8 月 10 日
Say I have this char vector:
'my.file.ext'
I want a regexprep command (not using fileparts) that adds a suffix, say '_old', to the file name before the extension, transforming it into
'my.file_old.ext'
If there is no dot (no extension), I'd like '_old' to be appended at the very end.
The following solution was shown to me here (incl. great explanations), and it works in Octave:
>> regexprep('my.file.ext', '^(?:(.*)(\.)|(.*))', '$3$1_old$2') % in Octave
ans = my.file_old.ext
However, even if the syntax of regexprep should be the same, it does not work in Matlab 9.6.0.1072779 (R2019a):
>> regexprep('my.file.ext', '^(?:(.*)(\.)|(.*))', '$3$1_old$2') % in Matlab R2019a
ans =
'$3$1_old$2ext'
Is Matlab's regexprep misbehaving or is it just different, and how?
In the latter case, what is a regexprep command that works in Matlab?
Thank you in advance.

採用された回答

Walter Roberson
Walter Roberson 2020 年 8 月 10 日
?: is "group but do not capture" so the () within it do not capture.
One approach:
regexprep('my.file.ext', {'^([^.]+)$', '^(.*)\.([^.]*)'}, {'$1_old', '$1_old.$2'})
  1 件のコメント
Felipe Jiménez Hernández
Felipe Jiménez Hernández 2020 年 8 月 10 日
It works! Thank you, Walter.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by