フィルターのクリア

Proper use of regexprep

2 ビュー (過去 30 日間)
GEORGIOS BEKAS
GEORGIOS BEKAS 2018 年 1 月 22 日
コメント済み: per isakson 2018 年 12 月 17 日
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
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
per isakson 2018 年 12 月 17 日
Your statement is lacking the square brackets. Try
s2 = regexprep(s1,'[qwrtpsdfghjklzxcvbnmQWRTPSDFGHKLZXCVBNM]','')

サインインしてコメントする。

採用された回答

KL
KL 2018 年 1 月 22 日
編集済み: KL 2018 年 1 月 22 日
use the ^ operator. It should simply be,
s2 = regexprep(s1,'[^aeiou]','')
documentation explains it clearly here: https://de.mathworks.com/help/matlab/ref/regexprep.html
  3 件のコメント
GEORGIOS BEKAS
GEORGIOS BEKAS 2018 年 1 月 22 日
also it removes the spaces and the capital letters. :/
KL
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
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?

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by