필터 지우기
필터 지우기

Regular Expression to extract bigram

조회 수: 4 (최근 30일)
arun
arun 2013년 9월 26일
답변: Andrei Bobrov 2013년 9월 26일
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일
EDIT
Do you want?
string = 'ab bc cd ef gh ij kl'
regexp(string,'\s+','split');
  댓글 수: 3
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일
z=regexp(string,'\w*','match')
strcat(z(1:end-1),{' '},z(2:end))

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by