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

9 Ansichten (letzte 30 Tage)
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?

Akzeptierte Antwort

the cyclist
the cyclist am 3 Jan. 2023
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 Kommentar
FM
FM am 3 Jan. 2023
Bearbeitet: FM am 4 Jan. 2023
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by