Java Regular expression matcher__ regular expression

Source: Internet
Author: User

There are 3 ways to do this when you use Mather for replacement:

1, ReplaceAll (str): Replace All

2, Replacefirst (str): Replace the first match to the

3, Appendreplacement (STRINGBUFFER,STR): This is a frequently used function, the first parameter is passed a stringbuffer, the second parameter is to specify the text to be replaced, The function can merge the replacement string into the original string and use it with Appendtail (StringBuffer SB) to achieve the substitution result we want, that is, the resulting string replaces only the one we want to replace, the other invariant.

As Example 1:

	public void Testreg () {
		String str1= "tttttttta\" b\ ca\ "tttttttt";
		Final String regexh1= "a\";
		Pattern pattern1 = Pattern.compile (regexh1,pattern.case_insensitive+pattern.dotall);
		Matcher matcher1 = Pattern1.matcher (str1);
		StringBuffer sb = new StringBuffer ();
		while (Matcher1.find ()) {
			matcher1.appendreplacement (SB, "OO");
		}
		 Matcher1.appendtail (SB);
		 System.out.println (Sb.tostring ());
	}

Example 2:

	public void TestReg2 () {
		String str1= "ABCDEFG";
		Final String regexh1= "ABC";
		Pattern pattern1 = Pattern.compile (regexh1,pattern.case_insensitive+pattern.dotall);
		Matcher matcher1 = Pattern1.matcher (str1);
		StringBuffer sb = new StringBuffer ();
		Replaces a character in a matched string, such as replacing B with @ while
		(Matcher1.find ()) {
			String matchstr = Matcher1.group () in the matching ABC;
			Matchstr = Matchstr.replaceall ("B", "@");
			Matcher1.appendreplacement (SB, MATCHSTR);
		 Matcher1.appendtail (SB);
		 System.out.println (Sb.tostring ());
	}


Report:

The Matcher method is as follows:

Matcher Appendreplacement (StringBuffer SB, String replacement)
Replaces the current matching substring with the specified string and adds the replacement substring and the string segment that precedes the last matching substring to a StringBuffer object.
StringBuffer Appendtail (StringBuffer SB)
Adds the remaining string to a StringBuffer object after the last matching job.
Int End ()
Returns the index position of the last character of the currently matched substring in the original target string.
Int End (int group)
Returns the position of the last character of a substring that matches the group specified in the match pattern.
Boolean Find ()
Attempts to find the next matching substring in the target string.
Boolean Find (int start)
Resets the Matcher object and attempts to find the next matching substring in the target string, starting at the specified location.
String Group ()
Returns the contents of all substrings that are obtained from the current lookup and that match the group
String Group (int group)
Returns the contents of a substring that matches the specified group for the current lookup.
Int GroupCount ()
Returns the number of matching groups that are available for the current lookup.
Boolean Lookingat ()
Detects whether the target string starts with a matching substring.
Boolean Matches ()
Attempts to expand the match detection for the entire target character, which means that the true value is returned only if the entire target string matches exactly.
Pattern Pattern ()
Returns the existing matching pattern for the Matcher object, which is the corresponding patterns object.
String ReplaceAll (String replacement)
Replaces all substrings that match the existing pattern with the specified string in the target string.
String Replacefirst (String replacement)
Replaces the first substring in the target string with the specified string as the one that matches the existing pattern.
Matcher Reset ()
Resets the Matcher object.
Matcher Reset (charsequence input)
Resets the Matcher object and specifies a new target string.
Int Start ()
Returns the position of the start character of the current lookup substring in the original destination string.
Int Start (int group)
Returns the position of the first character in the original destination string for the substring obtained by the current lookup and the specified group match.

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.