Regular expressions, words in a pattern

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

回答 (3 件)

Simon
Simon 2013 年 11 月 20 日

0 投票

Hi!
"\w" does not match spaces!

3 件のコメント

bububu
bububu 2013 年 11 月 20 日
still should capture for instance " have not recently run" which only has a word in between, right?
Simon
Simon 2013 年 11 月 20 日
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
bububu 2013 年 11 月 20 日
THANKS A LOT! IT WAS USEFUL!
Image Analyst
Image Analyst 2013 年 11 月 20 日

0 投票

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
bububu 2013 年 11 月 20 日

0 投票

I am looking for a certain pattern within a text. The pattern includes: i) do not run, ii) does not run, iii) has not run, iv) have not run or v) have not recently run. However, when I have more than one word between NOT and RUN, as in vi) do not go out or run, my pattern: pattern =' ((do(es)|ha(s|ve)|did|are)\s?no(t)\s?((\w+)\s|\s)?run*)'; does not find vi) do not go out or run with regexp. I would like to have not only characters between NOT and RUN, but also words... such that it can also find "do NOT go out or RUN"

この質問は閉じられています。

タグ

質問済み:

2013 年 11 月 20 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by