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 CenterFile Exchange 中查找有关 File Operations 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by