1 |
function theResult = findstr1(theText, theString) |
---|
2 |
|
---|
3 |
% findstr1 -- Find string, using * wildcard. |
---|
4 |
% findstr('demo') demonstrates itself. |
---|
5 |
% findstr1('theText', 'theString') searches 'theText' |
---|
6 |
% for all examples of 'theString', which may include |
---|
7 |
% one or more asterisks '*' as wildcards, representing |
---|
8 |
% zero or more characters. The sought-after string may |
---|
9 |
% also contain escaped characters, preceeded by backslash |
---|
10 |
% '\', and followed by one of '\bfnrt'. The routine |
---|
11 |
% returns or displays the indices of the starts of all |
---|
12 |
% qualifying strings. Not case-sensitive. |
---|
13 |
% |
---|
14 |
% Calls: none |
---|
15 |
|
---|
16 |
% Copyright (C) 2000 Dr. Charles R. Denham, ZYDECO. |
---|
17 |
% All Rights Reserved. |
---|
18 |
% Disclosure without explicit written consent from the |
---|
19 |
% copyright owner does not constitute publication. |
---|
20 |
|
---|
21 |
% Version of 04-Jan-2000 15:13:15. |
---|
22 |
% Updated 14-Jun-2001 05:35:04. |
---|
23 |
|
---|
24 |
% To do: |
---|
25 |
% |
---|
26 |
% 1. Need ? wildcard for single character. |
---|
27 |
% 2. Need $ for end of line. |
---|
28 |
|
---|
29 |
if nargout > 0, theResult = []; end |
---|
30 |
|
---|
31 |
if nargin < 1, theText = 'demo'; end |
---|
32 |
|
---|
33 |
if nargin < 2 & isequal(theText, 'demo') |
---|
34 |
help(mfilename) |
---|
35 |
theCommand = [mfilename '(''abracadabra'', ''a*b*a'')']; |
---|
36 |
disp(theCommand) |
---|
37 |
disp(mat2str(eval(theCommand))) |
---|
38 |
return |
---|
39 |
end |
---|
40 |
|
---|
41 |
if size(theText, 2) == 1, theText = theText.'; end |
---|
42 |
if size(theString, 2) == 1, theString = theString.'; end |
---|
43 |
|
---|
44 |
result = []; |
---|
45 |
|
---|
46 |
special = '\bfnrt'; |
---|
47 |
for i = 1:length(special) |
---|
48 |
s = ['\' special(i)]; |
---|
49 |
theString = strrep(theString, s, sprintf(s)); |
---|
50 |
end |
---|
51 |
|
---|
52 |
f = find(theString(1:end-1) == '*'); |
---|
53 |
if any(length(f) > 1) |
---|
54 |
theString(f(diff(f) == 1)) = ''; |
---|
55 |
end |
---|
56 |
|
---|
57 |
f = find(theString ~= '*'); |
---|
58 |
if any(f) |
---|
59 |
theString = theString(f(1):f(end)); |
---|
60 |
end |
---|
61 |
|
---|
62 |
theString = ['*' theString '*']; |
---|
63 |
stars = find(theString == '*'); |
---|
64 |
|
---|
65 |
if all(theString == '*') |
---|
66 |
result = 1:length(theText); |
---|
67 |
stars = []; |
---|
68 |
end |
---|
69 |
|
---|
70 |
len = 0; |
---|
71 |
|
---|
72 |
for i = 1:length(stars)-1 |
---|
73 |
s = theString(stars(i)+1:stars(i+1)-1); |
---|
74 |
len = len + length(s); |
---|
75 |
f = findstr(theText, s); |
---|
76 |
if isempty(f) |
---|
77 |
result = []; |
---|
78 |
elseif i == 1 |
---|
79 |
result = f; |
---|
80 |
elseif any(result) |
---|
81 |
okay = find(result+len <= max(f)+length(s)); % Careful. |
---|
82 |
result = result(okay); |
---|
83 |
end |
---|
84 |
if isempty(result), break, end |
---|
85 |
end |
---|
86 |
|
---|
87 |
if nargout > 0 |
---|
88 |
theResult = result; |
---|
89 |
else |
---|
90 |
disp(result) |
---|
91 |
end |
---|