regex: Extract then delete
조회 수: 17 (최근 30일)
이전 댓글 표시
Is there a regex function that can extract matched strings and delete them after? I would like to do this without searching over the string twice using regexp(str, expr, 'match') followed by regexprep(str, expr, '').
댓글 수: 0
채택된 답변
Stephen23
2019년 7월 4일
편집: Stephen23
2019년 7월 4일
"Is there a regex function that can extract matched strings and delete them after?"
Not really.
You could do something like this with regexprep and dynamic expressions to store the matched data in a workspace variable, but this will be slow, complex, and rather fragile.
One simple and efficient workaround would be to use both the match and split outputs: this example matches and removes the digits, leaving only the alphabetic characters:
>> S = 'abcd1234efghi6789jklm';
>> [X,Y] = regexp(S,'\d+','match','split');
>> Z = [Y{:}] % the new string with substrings removed
Z =
abcdefghijklm
>> X{:} % the matched substrings
ans =
1234
ans =
6789
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!