Regular expressions, words in a pattern
Información
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
Mostrar comentarios más antiguos
I am trying to generate a pattern including the following items:
do not run
does not run
has not run
have not run
and here comes the problem...
have not recently run
or
do not go out or run
so, what I am using is the following: '((do(es)|ha(s|ve)|did|are)\s?no(t)\s?(\w+)\s?run*)' trying to capture with '(\w+)\s' the fact that other words could be in between, but it does not work with words in between. The text is a string... Thanks in advance
Respuestas (3)
Simon
el 20 de Nov. de 2013
0 votos
Hi!
"\w" does not match spaces!
3 comentarios
bububu
el 20 de Nov. de 2013
Simon
el 20 de Nov. de 2013
It does in my test:
str = 'have not recently run';
pattern = '((do(es)|ha(s|ve)|did|are)\s?no(t)\s?(\w+)\s?run*)';
[a,b] = regexp(str, pattern, 'start', 'tokens');
Do you have more than one space in front of or behind "recently"? Then "\s?" doesn't match.
You may also remove the outer parentheses around your pattern to get all matches like:
str = 'have not recently run';
pattern = '(do(es)|ha(s|ve)|did|are)\s?no(t)\s?(\w+)\s?run*';
[a,b] = regexp(str, pattern, 'start', 'tokens');
b{:}
bububu
el 20 de Nov. de 2013
Image Analyst
el 20 de Nov. de 2013
0 votos
I'm very confused by what you want to do - I have no idea if you want to generate patterns from combinations of those phrases, or if you want to extract words or phrases in that list from larger sentences. Anyway (whatever you want to do), I think that either ismember () or allwords may be useful to you.
bububu
el 20 de Nov. de 2013
0 votos
La pregunta está cerrada.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!