Proper use of regexprep

3 views (last 30 days)
GEORGIOS BEKAS on 22 Jan 2018
Commented: per isakson on 17 Dec 2018
I want to remove the consonants of a string, using regexprep. How can I modify the initial string s1 with a string s2?
s2 = regexprep(s1,'qwrtpsdfghjklzxcvbnmQWRTPSDFGHKLZXCVBNM','')
per isakson
per isakson on 17 Dec 2018
Your statement is lacking the square brackets. Try
s2 = regexprep(s1,'[qwrtpsdfghjklzxcvbnmQWRTPSDFGHKLZXCVBNM]','')

Sign in to comment.

Accepted Answer

KL on 22 Jan 2018
Edited: KL on 22 Jan 2018
use the ^ operator. It should simply be,
s2 = regexprep(s1,'[^aeiou]','')
documentation explains it clearly here:
KL on 22 Jan 2018
it removes every character except what you mention inside the square brackets following ^ sign.
s2 = regexprep(s1,'[^aeiouA-Z]','') %ignores capital letters (A-Z)
s2 = regexprep(s1,'[^aeiouA-Z\s]','') %ignores white spaces as well
I gave you the link to documentation. It explains much more and guess what, even with examples!

Sign in to comment.

More Answers (1)

the cyclist
the cyclist on 22 Jan 2018
Edited: the cyclist on 22 Jan 2018
Can you just do
s1 = s2;
after that? Or just
s1 = regexprep(s1,'qwrtpsdfghjklzxcvbnmQWRTPSDFGHKLZXCVBNM','');
directly, eliminating creating the intermediate variable s2?


Find more on Characters and Strings in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!