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

조회 수: 9 (최근 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에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by