java 正则表达式 用法
在一个复杂的字符串中,使用 正则表达式 来取其中某个值
import java.util.regex.*;//正则表达式 引用 //复杂的字符串 String input="{\"pbxToken\":\"1ja930jsdlij912h94hk5l35poeweer\"}"+"{\"LS_CallStatus_Event_Type\":\"\",\"callId\":\"1701843953.1377\",\"callStatus_memberType\":\"Inbound\",\"inbound\":{\"callpath\":\"1234\",\"channelid\":\"PJSIP/trunk-MNWG-endpoint-00000397\",\"from\":\"13941128270\",\"memberstatus\":\"ALERT\",\"to\":\"1234\",\"trunkname\":\"MNWG\"},\"statusEnum\":\"OneMember_inboundAlert_callpath\",\"timeMillis\":1701843898676}---inbound---{\"callpath\":\"1234\",\"channelid\":\"PJSIP/trunk-MNWG-endpoint-00000397\",\"from\":\"13941128270\",\"memberstatus\":\"ALERT\",\"to\":\"1234\",\"trunkname\":\"MNWG\"}"; //使用正则表达式提取电话号码 //电话号码为 不带小数点的数字 Pattern pattern = Pattern.compile("\"from\":\"(\\d+)\""); Matcher matcher = pattern.matcher(input); if (matcher.find()) { String phoneNumber = matcher.group(1); logger.debug("====电话号码:" + phoneNumber ); } else { logger.debug("==== " +"未找到电话号码" ); }
//callId为 带小数点的数字 Pattern patternCallId = Pattern.compile("\"callId\":\"(\\d+\\.\\d+)\""); Matcher matcheCallId = patternCallId.matcher(input); if (matcheCallId.find()) { String callId = matcheCallId.group(1); System.out.println("提取的callId是:" + callId); } else { System.out.println("未找到callId"); }