Proper use of regexprep
3 次查看(过去 30 天)
显示 更早的评论
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','')
2 个评论
Guillaume
2018-1-22
I don't understand the question. Your code already remove the consonants (assuming basic latin alphabet only). What more do you want?
per isakson
2018-12-17
Your statement is lacking the square brackets. Try
s2 = regexprep(s1,'[qwrtpsdfghjklzxcvbnmQWRTPSDFGHKLZXCVBNM]','')
采纳的回答
KL
2018-1-22
编辑:KL
2018-1-22
use the ^ operator. It should simply be,
s2 = regexprep(s1,'[^aeiou]','')
3 个评论
KL
2018-1-22
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!
更多回答(1 个)
the cyclist
2018-1-22
编辑:the cyclist
2018-1-22
Can you just do
s1 = s2;
after that? Or just
s1 = regexprep(s1,'qwrtpsdfghjklzxcvbnmQWRTPSDFGHKLZXCVBNM','');
directly, eliminating creating the intermediate variable s2?
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!