1 | function results = globstrings(patterns, strs);
|
---|
2 | %GLOBSTRINGS String matching via wildcards.
|
---|
3 | % GLOBSTRINGS(PATTERNS,STRINGS) returns a cell array of the strings from
|
---|
4 | % STRINGS that match some wildcard pattern in PATTERNS.
|
---|
5 | % STRINGS is a cell array of strings.
|
---|
6 | % PATTERNS is a string or cell array of strings.
|
---|
7 | %
|
---|
8 | % Two types of wildcards are supported:
|
---|
9 | % * matches zero or more characters.
|
---|
10 | % ? matches exactly one character.
|
---|
11 | %
|
---|
12 | % Examples:
|
---|
13 | % globstrings('f?*r',{'fr','fur','four'}) % returns {'fur','four'}
|
---|
14 | % globstrings({'a*','*c'},{'ace','bar','rac'}) % returns {'ace','rac'}
|
---|
15 | %
|
---|
16 | % See also glob, glob2regexp.
|
---|
17 |
|
---|
18 | % Written by Tom Minka
|
---|
19 | % (c) Microsoft Corporation. All rights reserved.
|
---|
20 |
|
---|
21 | if ischar(patterns)
|
---|
22 | patterns = cellstr(patterns)';
|
---|
23 | end
|
---|
24 | patterns = patterns;
|
---|
25 | results = {};
|
---|
26 | for i = 1:length(patterns)
|
---|
27 | s = regexp(strs,glob2regexp(patterns{i}),'match');
|
---|
28 | s = cat(2,s{:});
|
---|
29 | if isempty(s)
|
---|
30 | warning([patterns{i} ' did not match anything']);
|
---|
31 | else
|
---|
32 | results = {results{:} s{:}};
|
---|
33 | end
|
---|
34 | end
|
---|