Strings: regular expressions

Keywords: Java REST less

5, split()

The split() method breaks the input string into an array of string objects, with the break boundary determined by the following regular expression:

String[] split(CharSequence input);
String[] split(CharSequence input, int limit);

This is a quick and convenient way to break the input text according to the general boundary:

import java.util.Arrays;
import java.util.regex.Pattern;

public class SplitDemo {
	public static void main(String[] args) {
		String input = "This!!unusual user!!of exclamation!!points";
		String[] split = Pattern.compile("!!").split(input);
		System.out.println(Arrays.toString(split));
		String[] split2 = Pattern.compile("!!").split(input, 3);
		System.out.println(Arrays.toString(split2));
	}
}

The second form of the split() method limits the amount of input that can be separated into strings.

6, Replace operation

Regular expressions are particularly convenient for replacing text, and provide many methods: replaceFirst(String replacement) replaces the first successful part of the match with the parameter string replacement. replaceAll(String replacement) replaces all parts that match successfully with the parameter string replacement. appendReplacement(StringBuffer sbuf, String replacement) performs incremental replacement instead of replacing only the first match or all matches as replaceFirst() and replaceAll(). This is a very important method. It allows you to call other methods to generate or process replacement (replaceFirst() and replaceAll() can only use a fixed string), which enables you to programmatically split the targets into groups, thus providing more powerful replacement functions. appendTail(StringBuffer sbuf). After one or more appendReplacement() operations, calling this method can copy the rest of the input string to sbuf.

The following procedure demonstrates how to use these alternatives:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TheReplacements {
	public static void main(String[] args) {
		String s = "AB!CD!EF!GH!IJ!K";
		System.out.println(s.replaceFirst("\\W", "@"));
		System.out.println(s.replaceAll("\\W", "@"));
		Matcher matcher = Pattern.compile("\\W").matcher(s);
		StringBuffer sbuf = new StringBuffer();
		while (matcher.find())
			matcher.appendReplacement(sbuf, "%");
		matcher.appendTail(sbuf);
		System.out.println(sbuf);
	}
}

The first two substitution methods use the method that comes with the String object. Here, it is more convenient to use this method. Note that if you use only one replacement, instead of compiling it as a Pattern, you can use the String replacement method directly, and the cost is less.

replaceFirst() replaces only the first match found. In addition, replaceFirst() and replaceAll() methods are only used to replace ordinary strings, so if you want to perform some special processing on these replacement strings, these two methods are not competent. If you want to do this, you should use the appendReplacement() method. This method allows you to manipulate the replacement string during the replacement process. In this example, sbuf is constructed to save the final result, and then a group is selected by group to process it, replacing the non word character of regular expression with%. In general, you should go through all the operations and then call the appendTail() method. However, if you want to simulate the behavior of replaceFirst (replace n times), you only need to perform a replacement, then call appendTail() to save the remaining unprocessed part into sbuf.

7, reset()

With the reset() method, you can apply an existing Matcher object to a new character sequence:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Resetting {
	public static void main(String[] args) {
		Matcher m = Pattern.compile("[frb][aiu][gx]").matcher("fix the rug with bags");
		while (m.find())
			System.out.print(m.group() + " ");
		System.out.println();
		m.reset("fix the rig with rags");
		while (m.find())
			System.out.print(m.group() + " ");
	}
}

Using the reset() method without parameters, you can reset the Matcher object to the beginning of the current character sequence.

If this article is of great help to you, please enjoy it.

Published 109 original articles, won praise 2, 10000 visitors+
Private letter follow

Posted by theslinky on Mon, 02 Mar 2020 19:54:27 -0800