Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- * @author <a href="mailto:strongant1994@gmail.com">strongant</a>
- * @since 2017/8/20
- */
- public class QuoteTypeMatch {
- public static void main(String[] args) {
- String msg = "PerformanceManager{第1个中括号}Product{第2个中括号}<{第3个中括号}79~";
- List<String> list = extractMessageByRegular(msg, "{", "}");
- for (int i = 0; i < list.size(); i++) {
- System.out.println(i + "-->" + list.get(i));
- }
- }
- /**
- * 使用正则表达式提取中括号中的内容
- *
- * @param msg
- * @return
- */
- public static List<String> extractMessageByRegular(String msg, String quoteTypeLeft, String quoteTypeRight) {
- if (null == msg || msg.length() == 0) {
- throw new IllegalArgumentException("指定的查找字符串不能为空!");
- }
- if (null == quoteTypeLeft || quoteTypeLeft.length() == 0) {
- throw new IllegalArgumentException("左边符号不能为空!");
- }
- if (null == quoteTypeRight || quoteTypeRight.length() == 0) {
- throw new IllegalArgumentException("右边符号不能为空!");
- }
- List<String> list = new ArrayList<>();
- Pattern p = Pattern.compile(String.format("\\%s.*?\\%s", quoteTypeLeft, quoteTypeRight));
- Matcher m = p.matcher(msg);
- while (m.find()) {
- list.add(m.group().substring(1, m.group().length() - 1));
- }
- return list;
- }
- }
Add Comment
Please, Sign In to add comment