Regular Expression to extract bigram

string = 'ab bc cd ef gh ij kl'
what will be the regular expression to extract bigram from the given string
I am writing the code
regexp(string,'\w* \w*','match');
the o/p is coming as: 'ab bc' 'cd' 'ef' 'gh' 'ij' 'kl'
while the output i am expecting as:
  • 'ab bc'
  • 'bc cd'
  • 'cd ef'
  • 'ef gh'
  • 'gh ij'
  • 'ij kl'

2 件のコメント

Walter Roberson
Walter Roberson 2013 年 9 月 26 日
I believe the term is "bi-gram".
If the string was
'abc defg'
would you want the result to be
ab bc c<space> <space>d de ef fg
or
ab de
or
ab bc de ef fg
?
Or does it only need to work on letter pairs ?
arun
arun 2013 年 9 月 26 日
yes you are saying right but i want to do it on word level and when i am writing the regexp as:
regexp(string,'\w+ \w+','match')
the o/p is: ans =
'ab bc' 'cd ef' 'gh ij'

サインインしてコメントする。

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 26 日
編集済み: Azzi Abdelmalek 2013 年 9 月 26 日

1 投票

EDIT
Do you want?
string = 'ab bc cd ef gh ij kl'
regexp(string,'\s+','split');

3 件のコメント

arun
arun 2013 年 9 月 26 日
編集済み: arun 2013 年 9 月 26 日
@Azzi Abdelmalek
No i want the
  • 'ab bc'
  • 'bc cd'
  • 'cd ef'
  • 'ef gh'
  • 'gh ij'
  • 'ij kl'as output
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 26 日
string = 'ab bc cd ef gh ij kl'
out=regexp(string,'\s+','split');
cellfun(@(x,y) [x ' ' y],out(1:end-1)', out(2:end)','un',0)
arun
arun 2013 年 9 月 26 日
Thanks @Azzi Abdelmalek
for your answer.

サインインしてコメントする。

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2013 年 9 月 26 日

1 投票

z=regexp(string,'\w*','match')
strcat(z(1:end-1),{' '},z(2:end))

カテゴリ

ヘルプ センター および File ExchangeData Type Identification についてさらに検索

タグ

質問済み:

2013 年 9 月 26 日

回答済み:

2013 年 9 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by