Brute force matching _java of Java pattern matching

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.