How to escape a portion of a string in regular expressions

10 ビュー (過去 30 日間)
Max Heimann
Max Heimann 2022 年 3 月 21 日
コメント済み: Max Heimann 2022 年 3 月 21 日
Hello,
i want to dynamically create a regular expression from an input string. However i only want a small subset of expressions to be treated as such. Most of the input shall be treated as literal (e.g. dots '.' shall actually be treated as dots and not wildcards)
Is there a way to escape a whole group/substring when defining a regexp, or is the only way to escape all special chars, except the ones i want to keep, manually. If i need to escape everything manually, is there a syntax/keyword which gets me all special chars in the string so i can escape all of them?
Example (dots shall be treated as literal dots):
>> r = myfun('a.b.c','a.b.')
r = 1
>> r_myfun('aXb.c','a.b.')
r = []
  4 件のコメント
Walter Roberson
Walter Roberson 2022 年 3 月 21 日
Perhaps the newer "pattern" facilities would help?
Max Heimann
Max Heimann 2022 年 3 月 21 日
Thats also great, but i need it to work in an earlier version than 2020.

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

採用された回答

Walter Roberson
Walter Roberson 2022 年 3 月 21 日
regexptranslate with the 'wildcard' option, perhaps?
  1 件のコメント
Max Heimann
Max Heimann 2022 年 3 月 21 日
This is great, thank you. I think i can work with this and use the 'escape' option. Afterwards i can look for the escaped versions of the patterns i want to match and de-escape them.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

製品


リリース

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by