Use regexp to find all letters in string except one

34 ビュー (過去 30 日間)
Assen Koev
Assen Koev 2024 年 1 月 24 日
コメント済み: Stephen23 2024 年 1 月 24 日
Is it possible to build a regexp search expression with an "intersection" of the default sets?
Simple conceptual example is the following:
Suppose I have a char 'abcd1 23 ,.@_'. (the char can include ANY character, not just the example ones)
I would like to return all LETTERS only except for 'd'.
In other words I need the search expression EXPR to be "\w except for d" or an intersection of \w and [^d]
Code example:
inputText = 'abcd1 23 ,.@_';
EXPR = ?
result = regexp(inputText,EXPR,'match')
{'a'} {'b'} {'c'}
Thank you
  3 件のコメント
Assen Koev
Assen Koev 2024 年 1 月 24 日
編集済み: Assen Koev 2024 年 1 月 24 日
Thanks Stephen
thanks for the correction I should have used a-zA-Z instead of \w
So, just to confirm that "You would have to list..." means constructions like the one in the link here is not possible?
I mean the format "[A-Z&&[^IVX]]+"?
Stephen23
Stephen23 2024 年 1 月 24 日
"constructions like the one in the link here is not possible?"
There are no such constructions shown in the MATLAB documentation:

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

採用された回答

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2024 年 1 月 24 日
This how it can be done:
inputText = 'abcd1 23 ,.@_';
EXPR = '[a-cA-Ce-zA-Z]';
result = regexp(inputText, EXPR, 'match');
disp(result);
{'a'} {'b'} {'c'}

その他の回答 (1 件)

Anton Kogios
Anton Kogios 2024 年 1 月 24 日
編集済み: Anton Kogios 2024 年 1 月 24 日
I copied your question (with no edits) into the MATLAB AI Chat Playground, and this was the response:
Yes, it is possible to build a regexp search expression with an "intersection" of the default sets in MATLAB. To return all letters except for 'd', you can use the following expression:
inputText = 'abcd1 23 ,.@_';
EXPR = '[a-ce-zA-Z]';
result = regexp(inputText, EXPR, 'match')
result = 1×3 cell array
{'a'} {'b'} {'c'}
The expression `[a-ce-zA-Z]` matches any letter from 'a' to 'c' and from 'e' to 'z' (both lowercase and uppercase). The result will be a cell array containing the matched letters: `{'a', 'b', 'c'}`.
I think that there is a small error in that EXPR should be '[a-ce-zA-CE-Z]' to get both lowercase and uppercase if you want it. I would also note that using \w as you mentioned would also include numbers (not just letters).
Hope this helps!

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by