Swapping Letters Using Regular Expression

2 Ansichten (letzte 30 Tage)
Michael Cappello
Michael Cappello am 12 Jul. 2019
Kommentiert: Michael Cappello am 12 Jul. 2019
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 Kommentare
Stephen23
Stephen23 am 12 Jul. 2019
Bearbeitet: Stephen23 am 12 Jul. 2019
"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 am 12 Jul. 2019
Agreed. But the problem statement was just an example that would help me understand the regular expression syntax.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume
Guillaume am 12 Jul. 2019
Bearbeitet: Guillaume am 12 Jul. 2019
>> text = {'Fred', 'Replace', 'Cannot'};
>> regexprep(text, '([^aeiou]*)([aeiou])(.*)', '$1$3$2')
ans =
1×3 cell array
{'Frde'} {'Rplacee'} {'Cnnota'}

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by