How to ascertain number of special characters in a string?

4 Ansichten (letzte 30 Tage)
Kelsey
Kelsey am 8 Nov. 2013
Kommentiert: Walter Roberson am 9 Nov. 2013
If there is array containing 5 different strings, how can I ascertain the number of special characters in each separate string?

Antworten (2)

Jeff E
Jeff E am 8 Nov. 2013
blah = ' This line contains * some & special ( chars_!@#';
idx = regexp(blah, '_|!|\*');
special_count = size(idx, 2);
  1 Kommentar
Walter Roberson
Walter Roberson am 8 Nov. 2013
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 "["

Melden Sie sich an, um zu kommentieren.


Azzi Abdelmalek
Azzi Abdelmalek am 8 Nov. 2013
s={'abc/d?' 'abcd' 'Abc123\'}
out=cellfun(@numel,regexp(s,'[^a-zA-A0-9]+','match'))
  3 Kommentare
Azzi Abdelmalek
Azzi Abdelmalek am 8 Nov. 2013
Bearbeitet: Azzi Abdelmalek am 8 Nov. 2013
s={'abc/d?' ' abé cd' 'Abc123\'}
out=cellfun(@numel,regexp(s,'[^\w\s]+','match'))
Walter Roberson
Walter Roberson am 9 Nov. 2013
\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)

Melden Sie sich an, um zu kommentieren.

Kategorien

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

Community Treasure Hunt

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

Start Hunting!

Translated by