Matlab examples of "regexp" where "expression" is a string array or cell array of characters?

6 ビュー (過去 30 日間)
The documentation for regexp seem to only illustrate use cases for which the "expression" argument is a single regular expression.
Are there online examples of use cases in which the "expression" argument consists of a string array or a cell array of characters?

採用された回答

the cyclist
the cyclist 2023 年 1 月 3 日
I'm not sure what you are looking for, but the function's behavior generalizes as you might expect:
str = "it is what it is";
out_single = regexp(str,"it")
out_single = 1×2
1 12
out_multi = regexp(str,["it";"is"])
out_multi = 2×1 cell array
{[1 12]} {[4 15]}
  1 件のコメント
FM
FM 2023 年 1 月 3 日
編集済み: FM 2023 年 1 月 4 日
Thanks, cyclist. I actually didn't have any preconceptions or expectations, which is why I found it odd that the documentation didn't describe this use case. I speculated that it may replace the regular expression "|" operator and allow for cleaner code, but I wasn't sure how to use it. Your example showed that my speculation was off track.
Afternote: I'm rediscovering the "contains" command, which seems to yield much more readable code for simple text searches. For regular expressions, however, I think there is benefit to "regexp" because it avoids the extra layer of knowledge needed to utilize Matlab "patterns".
Not that I find anything wrong with patterns. They are more readable than regular expressions in many cases. It's more a matter of introducing yet another way to do some of what regular expressions do, but not the more sophisticated stuff. With limited brain capacity, I'd rather go with one way.
I choose to go with regular expressions for a number of reasons. I already know regular expressions. They are more powerful. There is more documentation on them. Regular expression skills are transferrable across platforms/environments. And consequently, you can leverage experience in regular expressions from other platforms/environments.
These benefits of regular expressions do not necessarily hold for everyone, so I can see how Matlab patterns have a place.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by