MATLAB: If Regexp matches return 1 otherwise 0 syntax

MATLABregexp syntax

Hi,
Right now I'm using the following to get a boolean result from a regexp. It just doesn't feel right – is there a better way to do this?
(size(regexp(myInput,myPattern),1)>0)

Best Answer

if regexp(myInput,myPattern)
regexp() by default returns a list of indices upon a match, and [] if there are no matches. The list of indices will all be non-zero numbers, and "if" applied to an array of non-zero numbers is considered to be true, just as if all() had been applied to the list. "if" applied to the empty matrix is false. So, you do not need to do any conversion: you can just test regexp() result directly.