Here you can find the source of getAllMatches(String[] target, String[] pattern)
public static ArrayList<Integer> getAllMatches(String[] target, String[] pattern)
//package com.java2s; /**/*from w ww . j a v a 2s. c o m*/ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. * * @author Wei Zhang, Language Technology Institute, School of Computer Science, Carnegie-Mellon University. * email: wei.zhang@cs.cmu.edu * */ import java.util.ArrayList; public class Main { /** * Input: array of strings, and a target array Output: the array of all the appearance */ public static ArrayList<Integer> getAllMatches(String[] target, String[] pattern) { ArrayList<Integer> result = new ArrayList<Integer>(); int start = 0; while (start < target.length) { String[] subArray = new String[target.length - start]; for (int iter = 0; iter < subArray.length; iter++) { subArray[iter] = target[iter + start]; // System.out.print(subArray[iter]+" "); } // System.out.println(); int index = getFirst(subArray, pattern); if (index != -1) { result.add(start + index); start = start + index + pattern.length; } else return result; } return result; } /** * Input: array of strings, and a target array Output: the first appearance position */ public static int getFirst(String[] target, String[] pattern) { for (int i = 0; i < target.length; i++) target[i] = target[i].trim().toLowerCase(); for (int i = 0; i < pattern.length; i++) pattern[i] = pattern[i].trim().toLowerCase(); int tlen = target.length; int plen = pattern.length; int itarget = 0, ipattern = 0; while (itarget != tlen) { if ((itarget + plen) >= tlen) return -1; while (ipattern < plen) { if (pattern[ipattern].equals(target[itarget + ipattern])) { if (ipattern == (plen - 1)) return itarget; else { ipattern++; } } else break; } itarget++; ipattern = 0; } return -1; } }