Swapping Letters Using Regular Expression

조회 수: 3 (최근 30일)
Michael Cappello
Michael Cappello 2019년 7월 12일
댓글: Michael Cappello 2019년 7월 12일
I want to take a character array, find the first vowel and move it to the end. Some examples
  • 'Fred' --> 'Frde'
  • 'Replace' --> 'Rplacee'
  • 'Cannot' --> 'Cnnota'
Is there a regular expression that will accomplish this with a single call to regexp?
Thank you
  댓글 수: 2
Stephen23
Stephen23 2019년 7월 12일
편집: Stephen23 2019년 7월 12일
"Is there a regular expression that will accomplish this with a single call to regexp?"
There is likely no simple regular expression that will reliably detect the first vowel of every word in the entire English corpus, due to some letters which indicate vowels in some words and consonants in others (particularly w, u, and y).
Like most categorizations, it gets very blurred once you start looking in detail...
What is the first vowel of 'Queen' ?
What about 'Year' ?
Cwm ?
Gym ?
But not Yesterday...
Michael Cappello
Michael Cappello 2019년 7월 12일
Agreed. But the problem statement was just an example that would help me understand the regular expression syntax.

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2019년 7월 12일
편집: Guillaume 2019년 7월 12일
>> text = {'Fred', 'Replace', 'Cannot'};
>> regexprep(text, '([^aeiou]*)([aeiou])(.*)', '$1$3$2')
ans =
1×3 cell array
{'Frde'} {'Rplacee'} {'Cnnota'}

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by