Java pattern matching and the corresponding example

Source: Internet
Author: User

public class patternmatching { /*  *  This method is used to determine if a substring exists in the parent string   If there is a return true, Otherwise, False searchme represents the parent string, and substring represents the substring   */ public boolean match (string  Searchme, string substring)  { boolean flag = true; // flag represents a tag variable, Used to determine if the match succeeds  int max = searchme.length ()  - substring.length ();// max is offset  for  (int i = 0; i <= max; i++)  { flag =  true; for  (Int j = 0; j < substring.length ();  j++)  {  if  (Searchme.charat (I + j)  != substring.charat (j))  { flag =  false;//  Description Matching failed  } } if  (flag)  {//  description match success  break; } }  return flag; } /*  This method is used to print the index where the substring first appears in the parent string  */ public int  Matchindex (STRING&NBSP;SEARCHME,&NBSP;STRING&NBSp;substring)  { boolean flag = true; // flag represents a tag variable that is used to determine if the match is successful  int  index = 0;// index This variable is used to represent the first index position of a substring in the parent string  int max = searchme.length ( )  - substring.length ();// max is offset  for  (int i = 0; i <=  max; i++)  { flag = true; for  (int j = 0; j  < substring.length ();  j++)  { if  (Searchme.charat (i + j)  !=  Substring.charat (j))  { flag = false;//  description match failed  } } if  (flag)   {//  Description Match Successful  index = i; break; } } return index; } /*   This method is used to find all locations where the substring appears in the parent string  */ public stringbuffer matchallindex (String searchme,  string substring)  { boolean flag = true; // flag represents a tag variable, Used to determine if the match was successful  int max =  Searchme.length ()  - substring.length ();// max is offset  stringbuffer index = new  stringbuffer ("All locations where the substring appears in the parent string is:"); for  (int i = 0; i <= max;  i++)  { flag = true; for  (int j = 0; j <  Substring.length ();  j++)  { if  (Searchme.charat (i + j)  !=  Substring.charat (j))  { flag = false;//  description match failed  } } if  (flag)   {//  Description Match Success  index.append (i +  " ");//  Use StringBuffer-specific method append to stitch all index positions of the substring in the first letter of the parent string  continue; } } return index; }  /*  This method is used to replace substrings in the parent string with the specified string */ public string replace (string searchme, string  substring,string replacation) { boolean flag = true; // flag represents a tag variable, Used to determine if the match succeeded  int max = searchme.length ()  - substrinG.length ();// max is offset  for  (int i = 0; i <= max; i++)  { flag = true; for  (int j = 0; j <  Substring.length ();  j++)  { if  (Searchme.charat (i + j)  !=  Substring.charat (j))  { flag = false;//  description match failed  } } if  (flag)   {//  Description Match Success  searchme = searchme.replace (substring, replacation);  continue; }  }  return searchme;  }}


Here is the test class: Atternmatchingtest

public class Patternmatchingtest {public static void main (string[] args) {String fatherstr = ' Look for a ' substring in me substring ";//Parent String sonstr =" subs ";//substring string replacation =" AAAA ";  /* This print is used to determine if the match is successful */SYSTEM.OUT.PRINTLN (new Patternmatching (). Match (Fatherstr, sonstr));  /* This print is used to print out the index where the substring first appears in the parent string */System.out.println (new Patternmatching (). Matchindex (Fatherstr, sonstr));  /* This print is used to print all locations where the substring appears in the parent string */System.out.println (new Patternmatching (). Matchallindex (Fatherstr, sonstr)); /* This print is used to replace substrings in the parent string with the specified string */System.out.println (new Patternmatching (). replace (Fatherstr, SONSTR, replacation)); }}


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.