How to ascertain number of special characters in a string?

조회 수: 1 (최근 30일)
Kelsey
Kelsey 2013년 11월 8일
댓글: Walter Roberson 2013년 11월 9일
If there is array containing 5 different strings, how can I ascertain the number of special characters in each separate string?

답변 (2개)

Jeff E
Jeff E 2013년 11월 8일
blah = ' This line contains * some & special ( chars_!@#';
idx = regexp(blah, '_|!|\*');
special_count = size(idx, 2);
  댓글 수: 1
Walter Roberson
Walter Roberson 2013년 11월 8일
I would simplify the pattern to '[_!*]' instead of '_|!|\*'
If you do use [] then if you have "-" (dash, minus sign) in the list, ensure that it is either right after the "[" or right before the "[". Also if you include "^" in the list, ensure that it is not right after the "["

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


Azzi Abdelmalek
Azzi Abdelmalek 2013년 11월 8일
s={'abc/d?' 'abcd' 'Abc123\'}
out=cellfun(@numel,regexp(s,'[^a-zA-A0-9]+','match'))
  댓글 수: 3
Azzi Abdelmalek
Azzi Abdelmalek 2013년 11월 8일
편집: Azzi Abdelmalek 2013년 11월 8일
s={'abc/d?' ' abé cd' 'Abc123\'}
out=cellfun(@numel,regexp(s,'[^\w\s]+','match'))
Walter Roberson
Walter Roberson 2013년 11월 9일
\w Any alphabetic, numeric, or underscore character. For English character sets, \w is equivalent to [a-zA-Z_0-9]
Notice that includes underscore.
Also the wording there hints that what \w matches might be locale-dependent, such as LANG=en-CA (Canadian English) perhaps having \w match something different than LANG=en-FR (Canadian French)

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by