deepreplace: Replace string segments in nested objects

版本 1.0.1.0 (146.0 KB) 作者: George Abrahams
NEWDATA = DEEPREPLACE(DATA,MATCH,REPLACEMENT) replaces all occurrences of substring(s) MATCH with REPLACEMENT at any level of object DATA.
2.0 次下载
更新时间 2023/1/6

deepreplace: Find and replace string segments in nested objects

View on GitHub View on File Exchange

Summary

newdata = deepreplace(data,match,replacement) replaces all occurrences of substring(s) match with replacement at any level of the nested object data.

Inputs

  • data can be of any class but will only undergo replacement when it contains text, i.e. string array or character vector, at any hierarchical level.
  • match must be a text or pattern array.
  • replacement must be text. It must be the same size as match or be scalar.

Output

  • newdata is the same class as data. All nonoverlapping occurrences of each element of match in data are replaced by the corresponding element of replacement.

Examples

DATA = { '2022-12-8', 5; '2022-12-9', 9 };
MATCH = [ "-", "2022" ];
REPLACEMENT = [ "/", "22" ];
deepreplace( DATA, MATCH, REPLACEMENT )
-----
ans = 2×2 cell array:
    {'22/12/8'} {[5]}
    {'22/12/9'} {[9]}
username = 'Default';
Directories.matlab = { 'C:\Program Files\MATLAB', ...
    'C:\Users\<user>\Documents\MATLAB' };
Directories.labrat = 'C:\Users\<user>\Documents\LABRAT';
Directories = deepreplace( Directories, '<user>', username )
-----
Directories = struct with fields:
    matlab: { 'C:\Program Files\MATLAB', ...
                'C:\Users\Default\Documents\MATLAB' }
    labrat: 'C:\Users\Default\Documents\LABRAT'

More examples available in examples.mlx / examples.pdf.

Compatibility, License and Citation

Created in 2022b. Compatible with MATLAB release 2019b and later. Compatible with all platforms.

Published under MIT License (see LICENSE.txt).

Please cite George Abrahams (https://github.com/WD40andTape/fieldfun, https://www.linkedin.com/in/georgeabrahams).

引用格式

George Abrahams (2022) (https://www.linkedin.com/in/georgeabrahams). deepreplace: Replace string segments in nested objects (https://github.com/WD40andTape/deepreplace/releases/tag/v1.0.0), GitHub. Retrieved December 30, 2022.

MATLAB 版本兼容性
创建方式 R2022b
与 R2019b 及更高版本兼容
平台兼容性
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!
版本 已发布 发行说明
1.0.1.0

See release notes for this release on GitHub: https://github.com/WD40andTape/deepreplace/releases/tag/v1.0.1

1.0.0

要查看或报告此来自 GitHub 的附加功能中的问题,请访问其 GitHub 仓库
要查看或报告此来自 GitHub 的附加功能中的问题,请访问其 GitHub 仓库