String解析工具类-StringParseUtil
============================================================================String解析工具类:
import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class StringParseUtil { /**解析获取{}内容 * @param input * @return */ public static List<String> parseBetween3(String input) { if(StringCheckUtil.isEmpty(input)) { return new ArrayList<>(); } String pattern = "\\{([^\\}]*)\\}"; Matcher matcher = Pattern.compile(pattern).matcher(input); List<String> strList = new ArrayList<>(); while (matcher.find()) { String output = matcher.group(1); strList.add(output); } return strList; } /**解析获取[]内容 * @param input * @return */ public static List<String> parseBetween2(String input) { if(StringCheckUtil.isEmpty(input)) { return new ArrayList<>(); } String pattern = "\\[([^\\]]*)\\]"; Matcher matcher = Pattern.compile(pattern).matcher(input); List<String> strList = new ArrayList<>(); while (matcher.find()) { String output = matcher.group(1); strList.add(output); } return strList; } /**解析获取()内容 * @param input * @return */ public static List<String> parseBetween(String input) { if(StringCheckUtil.isEmpty(input)) { return new ArrayList<>(); } String pattern = "\\(([^\\)]+)\\)"; Matcher matcher = Pattern.compile(pattern).matcher(input); List<String> strList = new ArrayList<>(); while (matcher.find()) { String output = matcher.group(1); strList.add(output); } return strList; } }
============================================================================String解析工具测试类:
/** * 解析获取()内容 */ @Test public void test_parseBetween() { String input = "(test) data (测试)(data)"; System.out.println(StringParseUtil.parseBetween(input)); String input2 = "[test] {data} [测试](data)"; System.out.println(StringParseUtil.parseBetween2(input2)); String input3 = "{test} {data} {测试}(data)"; System.out.println(StringParseUtil.parseBetween3(input3)); }