截取字符串中的单引号或者双引号的数据

package com.shine.eiuop.utils;

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

public class test {

public static void main(String[] args) {
// TODO Auto-generated method stub
// 查找的字符串
String line = "GET_DEP_INFO : \"InfoSendService.getDeptInfo\",";

String line2 = "QUERY_DETAIL:'InfoSendService2.getMsgInfoDetailDTOByInfoId',";
System.out.println(line.matches("(.*)Service.(.*)"));
//方法1:
//截取字符串中的单引号或者双引号的数据
System.out.println((line.split("\\.")[0].substring(line.split("\\.")[0].indexOf("\"")+1)).trim());
System.out.println((line2.split("\\.")[0].substring(line2.split("\\.")[0].indexOf("\'")+1)).trim());


//方法2:
//使用正匹配字符串中含有双引号
// 创建 Pattern 对象
Pattern p1=Pattern.compile("\"(.*?)\"");
// 创建 matcher 对象
Matcher m = p1.matcher(line);
while (m.find()) {
System.out.println("Found value: " +m.group().trim() );
System.out.println("Found value0: " + m.group(0) );
System.out.println("Found value1: " + m.group(1) );
}

}

}


//最终输出:
true
InfoSendService
InfoSendService2
Found value: "InfoSendService.getDeptInfo"
Found value0: "InfoSendService.getDeptInfo"
Found value1: InfoSendService.getDeptInfo

posted @ 2020-07-23 01:57  rearboal  阅读(3026)  评论(0编辑  收藏  举报