フィルターのクリア

Search for elements in a string

1 回表示 (過去 30 日間)
Bob Whiley
Bob Whiley 2015 年 1 月 28 日
コメント済み: Stephen23 2015 年 2 月 1 日
I am trying to find whether a certain string of letters and special characters ( like /) and then output true if it does and false if it doesn't. I know I should convert the characters to numbers using double() but after that I am not sure what to do. An example would be to see if 'add?/adfd' has any elements from ascii value 32-47.
  1 件のコメント
Cedric
Cedric 2015 年 1 月 28 日
編集済み: Cedric 2015 年 1 月 28 日
Could give an example of text and string that you need to find? There are several tools available, e.g. STRFIND and REGEXP. the former is easy to understand and use, but limited, and the latter is powerful but we would need to know more about the type of strings that you need to find before we can propose a solution.

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

採用された回答

Stephen23
Stephen23 2015 年 1 月 28 日
編集済み: Stephen23 2015 年 1 月 28 日
Why make it all complicated with regexp. Keep it simple and very MATLAB with bsxfun :
any(any(bsxfun(@eq,'add?/adfd',[32:47].')))
ans = true
The second input to bsxfun is your string, and the third is a column vector of the ascii values that you want to check for.

その他の回答 (5 件)

Chad Greene
Chad Greene 2015 年 1 月 28 日
For multiple special characters, use a few calls of regexp and any:
s = 'abcd\potatoes\eddie\murphy?martin/lawrence';
specialChars = [regexp(s,'\.') regexp(s,'&') regexp(s,'/') regexp(s,'^')];
if any(specialChars)
disp('Yeah, they''re some special characters alright.')
end
  3 件のコメント
Chad Greene
Chad Greene 2015 年 1 月 28 日
brilliant!
Guillaume
Guillaume 2015 年 1 月 28 日
And even, if it is, a single regexp still suffice:
s = 'abcd\potatoes\eddie\murphy?martin/lawrence';
[pos, match] = regexp(s, '[\\?/]', 'start', 'match');
[characters, ~, subs] = unique(match);
positions = accumarray(subs, pos, [], @(p) {p})';
[characters; positions]

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


Chad Greene
Chad Greene 2015 年 1 月 28 日
Use regexp
s = 'abcd\potatoes\eddie\murphy';
indicesOfSlash = regexp(s,'\')
indicesOfSlash =
5 14 20

Image Analyst
Image Analyst 2015 年 1 月 28 日
Here's a method that (in my opinion) is a little more intuitive, straightforward, and less cryptic than regexp() and bsxfun():
s = 'add?/adfd' % The starting string
d = s - ' ' + 32 % Convert to ASCII number array
% Get indexes of where d is between 32 and 47
specialCharIndices = find(d >= 32 & d <= 47)
  3 件のコメント
Image Analyst
Image Analyst 2015 年 1 月 29 日
logicalIndices = d >= 32 & d <= 47;
Stephen23
Stephen23 2015 年 2 月 1 日
"a little more intuitive, straightforward, and less cryptic", that is until you want to compare more than one contiguous group of characters:
(s>=32 & s<=47) | (s>=58 & s<=60) | (s>=62 & s<=64) | (s>=133 & s<=140) % etc
bsxfun(@eq,'add?/adfd',[32:47,58:60,62:64,133:140].')
The bsxfun solution is much more robust in comparing any two input vectors, regardless of the contiguity or later decisions about what characters to include/exclude.

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


per isakson
per isakson 2015 年 1 月 28 日
編集済み: per isakson 2015 年 1 月 29 日
A code based on regexp
str = 'add\?/adfd';
chr = char(32:47);
xpr = ['[',regexptranslate('escape',chr),']'];
pos = regexp( str, xpr, 'once' );
has = not(isempty(pos));
This code may be squeezed into one obscure line.

Stephen23
Stephen23 2015 年 2 月 1 日
How about the simplest solution of all isstrprop, which lets you do something like this all in one line:
>> ~isstrprop('add?/adfd','alpha')
ans =
0 0 0 1 1 0 0 0 0

カテゴリ

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