How to escape a portion of a string in regular expressions

조회 수: 16 (최근 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 CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품


릴리스

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by