Download code

Jump to: navigation, search

Back to Matching_with_wildcards_(Java)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

MatchingWithWildcards.java

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Matching_with_wildcards_(Java)?oldid=12043
14 */
15 
16 
17 import static org.junit.Assert.assertEquals;
18 import org.junit.Test;
19 
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collection;
23 
24 public class MatchingWithWildcards {
25 	public static Collection<String> getMatches(String t, String p) {
26 		Collection<String> result = new ArrayList<String>();
27 		for (int i = 0; i < t.length(); i++) {
28 			
29 			int j = 0;
30 			int h = i;
31 			int n = p.length();
32 
33 			while (true){
34 				
35 				int L = SimpleLongestCommonExtension.longestCommonExtension(p, j, t, h);
36 				
37 				if (j + 1 + L == n + 1) {
38 				    result.add(t.substring(i, i + n));
39 				    break;
40 				}
41 				
42 				if (((j + L) < p.length() && p.charAt(j + L) == '*')
43 				        || ((h + L) < t.length() && t.charAt(h + L) == '*')) {
44 				    j = j + L + 1;
45 				    h = h + L + 1;
46 				} else
47 				    break;
48 			}
49 		}
50 		return result;
51 	}
52 	@Test
53 	public void testGetMatches() {
54 		
55 		Collection<String> results = getMatches("abentbananaend bend", "ben*");
56 		assertEquals(Arrays.asList("bent", "bend"), results);
57 	}
58 }


hijacker
hijacker
hijacker
hijacker

SimpleLongestCommonExtension.java

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Matching_with_wildcards_(Java)?oldid=12043
14 */
15 
16 
17 import static org.junit.Assert.assertEquals;
18 import org.junit.Test;
19 
20 public class SimpleLongestCommonExtension {
21 	
22 	public static int longestCommonExtension(String t1, int i1, String t2, int i2) {
23 	    int res = 0;
24 	    for (int i = i1; i < t1.length() && i2 < t2.length(); i++, i2++) {
25 	        if (t1.charAt(i) == t2.charAt(i2))
26 	            res++;
27 	        else
28 	            return res;
29 	    }
30 	    return res;
31 	}
32 
33 	
34 	@Test
35 	public void testGetLongestCommonExtension() {
36 	    int res = longestCommonExtension("zsdabcdefghj", 3, "abcdezas", 0);
37 	    assertEquals(5, res);
38 	}
39 }


hijacker
hijacker
hijacker
hijacker