If Regexp matches return 1 otherwise 0 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)

 Respuesta aceptada

Walter Roberson
Walter Roberson el 25 de Mzo. de 2013
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.

3 comentarios

Doesn't seem to work with other boolean operators, try:
regexp('aa00aa00','[0-9]+') && 1
Because two returns are given, it is unhappy, thus the size function, which seems awkward.
Walter Roberson
Walter Roberson el 25 de Mzo. de 2013
Editada: Walter Roberson el 25 de Mzo. de 2013
~isempty(regexp('aa00aa00', '\d+')) && 1
or
any(regexp('aa00aa00', '\d+')) && 1
Note: all() instead of any() will not work.
Michael
Michael el 25 de Mzo. de 2013
Thanks

Iniciar sesión para comentar.

Más respuestas (4)

gwoo
gwoo el 23 de Ag. de 2021
Editada: gwoo el 8 de Feb. de 2023
If your input to regex is a cell array (say from collecting from a struct or something), then your output will be a cell array which is not immediately able to be used for logical indexing. You need to convert it from a cell array to a logical array. But many times you'll get empty cells so you can't just use cell2mat because that will implicitly ditch the empty cells and only leave you with the non-empty which doesn't help for indexing. Therefore, I use this following approach to go from an input of a cell array to an output of a logical array.
This is how I get a logical array out of regex:
logicalMatches = ~cellfun('isempty', regexpi({filesInDir.name}, stringToBeFound, 'once'));

1 comentario

James Van Zandt
James Van Zandt el 12 de Mayo de 2022
I have a cell array of strings to test, so I used this method to collect the matches.
K>> ca={'able','baker','charlie','delta','echo','fox','golf','hotel'}
ca =
1×8 cell array
{'able'} {'baker'} {'charlie'} {'delta'} {'echo'} {'fox'} {'golf'} {'hotel'}
K>> regexp(ca,'a')
ans =
1×8 cell array
{[1]} {[2]} {[3]} {[5]} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
K>> ~cellfun('isempty',regexp(ca,'a'))
ans =
1×8 logical array
1 1 1 1 0 0 0 0

Iniciar sesión para comentar.

Dani A
Dani A el 17 de Dic. de 2025
Editada: Dani A el 17 de Dic. de 2025
a more modern solution:
% for reproducibility
rng(0);
% 10 strings
strSize = [10 1];
% 10 characters in each string
numChars = 10;
% only lowercase Latin characters
unicodeBnds = [97 122];
%
myInput = arrayfun(@(a) string(char(randi(unicodeBnds,[1 numChars]))),ones(strSize))
myInput = 10×1 string array
"vxdxqchoyz" "ezymudkxuy" "rawyrttkre" "sahbcvsiya" "ljtuemlqst" "hrredmyipf" "tgnsxyoddg" "vgvgyjfgqm" "jvpoxhttjo" "bbnuydomai"
myPattern = "[abc]";
contains(myInput,regexpPattern(myPattern))
ans = 10×1 logical array
1 0 1 1 0 0 0 0 0 1

4 comentarios

this is nice but doesn't work on a cell array unless you use:
cellfun(@(x) contains(x, regexpPattern(myPattern)), myInput)
Unrecognized function or variable 'myInput'.
Walter Roberson
Walter Roberson el 17 de Dic. de 2025
The original solution (size(regexp(myInput,myPattern),1)>0) was also not generalizable to cell arrays myInput so I do not think this was a consideration for the original poster.
Dani A
Dani A el 17 de Dic. de 2025
Editada: Dani A el 17 de Dic. de 2025
@gwoo if `myInput` is a properly formatted cell string, then my solution should work. I suspect that you had a cell array of 1-by-1 strings, rather than a cell array of character vectors.
% for reproducibility
rng(0);
% 10 strings
cellSize = [10 1];
% 10 characters in each string
numChars = 10;
% only lowercase Latin characters
unicodeBnds = [97 122];
%
myInput = cellfun(@(c) char(randi(unicodeBnds,[1 numChars])),repmat({1},cellSize),'UniformOutput',false)
myInput = 10×1 cell array
{'vxdxqchoyz'} {'ezymudkxuy'} {'rawyrttkre'} {'sahbcvsiya'} {'ljtuemlqst'} {'hrredmyipf'} {'tgnsxyoddg'} {'vgvgyjfgqm'} {'jvpoxhttjo'} {'bbnuydomai'}
myPattern = '[abc]';
contains(myInput,regexpPattern(myPattern))
ans = 10×1 logical array
1 0 1 1 0 0 0 0 0 1
gwoo
gwoo el 18 de Dic. de 2025
@Walter Roberson yes that's a good point.
@Dani A wasn't even thinking of that. thank you!

Iniciar sesión para comentar.

Azzi Abdelmalek
Azzi Abdelmalek el 25 de Mzo. de 2013

0 votos

Maybe you want to use isequal or strcmp

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 25 de Mzo. de 2013

Comentada:

el 18 de Dic. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by