Brute-Force match for Java pattern matching
/** * Pattern matching brute force matching/package javay.util; /** * Pattern Match Brute-force * @author DBJ/public class PMBF {/** * match Brute-force * @param Target Target String * @param pattern Mode String * @return mode string for the first occurrence in the target string/public static int Patternmatch (String target, St
Ring pattern) {int targetlength = Target.length ();
int patternlength = Pattern.length (); int idxtgt = 0; The position of the character in the target string int idxptn = 0; The position of the character in the pattern string int index = 0;
Saves the position of the starting character of ING with the pattern string while (Idxtgt < targetlength && IDXPTN < Patternlength) {//Find a matching character
if (Target.charat (idxtgt) = = Pattern.charat (IDXPTN)) {//If equal, continue the subsequent comparison of characters idxtgt + +;
IDXPTN + +;
else {//otherwise the target string begins with the second character to compare Index + + with the first character of the pattern string;
IDXPTN = 0;
IDXTGT = index;
}///Match to one, output result if (IDXPTN = = patternlength) {//Description match successful return index;
else {return-1;
}
}
}
Use examples:
static int indexOf (char[] source,char[] Target {char-I
= target[0];
int max = (source.length-target.length);
for (int i = 0; I <= max; i++) {/* Look for the
character/
if (Source[i]!= a) {while ++i
Max && Source[i]!= A;
/* Found-character, now look at the rest of V2
/if (i <= max) {
int j = i + 1;
int end = j + target.length-1;
for (int k = 1; J < end && Source[j] = = Target[k]; j + +, k++);
if (j = = end) {/
* Found whole string. */Return
i
}}} return-1;
}
The above mentioned is the entire content of this article, I hope you can enjoy.