RegExp to remove code between triangle brackets <>
4 ビュー (過去 30 日間)
古いコメントを表示
I want to remove text from a string contained within triangle brackets:
string1 = '<asdfasdf> text <asdf> <";>';
string2 = 'text';
I'm trying to use regexprep to remove the bracketed text, or regexp to extract non-bracketed text. Below code does not correctly identify all the bracketed strings.
string1 = '<asdfasdf> text <asdf> <";>';
exp = '^<.*>$';
ind = regexp(string1,exp);
1 件のコメント
採用された回答
Les Beckham
2025 年 8 月 28 日
編集済み: Les Beckham
2025 年 8 月 28 日
Try using non-greedy matching (with a question mark: ?). This works on your example (assuming you want to remove the brackets as well as the text between them):
string1 = '<asdfasdf> text <asdf> <";>';
exp = '<.*?>';
newstring = regexprep(string1, exp, '')
See documentation here
0 件のコメント
その他の回答 (1 件)
Walter Roberson
2025 年 8 月 28 日
This question cannot be answered meaningfully unless we know the exact rules by which it is legal to have additional "<" and ">" characters within the string contained in triangle brackets.
For example,
string1 = '<asdf<sam>asdf> text <asdf> <";>';
string2 = '<asdf<asdf> text <asdf> <";>';
string3 = '<asdf>asdf> text <asdf> <";>';
string4 = '<asdf<sa<foo>m>asdf> text <asdf> <";>';
string5 = '<asdf<s<a<asdf> text <asdf> <";>';
string6 = '<asdf>s>a>asdf> text <asdf> <";>';
are all potentially valid "characters within the string contained in triangle brackets". Allowing nested <> complicates regexp parsing a lot, especially if there is no upper limit on the number of nesting levels permitted.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!