我想要一个Java正则表达式:
ACK:2018-03-08 12:12:28:4802:RETN = 0,ID = 1,NUM = 9,RESULT = 290,1," sip:99990030@192.30.32.68" ;," **********"" "" 99990030",1,31,106," 0.0.0.0",0&安培; 291,1," SIP:99990031@192.30.32.68" ;," **********"" "" 99990031",1,31,101," 0.0.0.0",0&安培; 292,1," SIP:99990032@192.30.32.68" ;," **********"" "" 99990032",1,31,101," 0.0.0.0",0&安培; 293,1," SIP:99990033@192.30.32.68" ;," **********"" "" 99990033",1,31,101," 0.0.0.0",0&安培; 294,1," SIP:99990034@192.30.32.68" ;," **********"" "" 99990034",1,31,101," 0.0.0.0",0&安培; 295,1," SIP:99990035@192.30.32.68" ;," **********"" "" 99990035",1,31,101," 0.0.0.0",0&安培; 296,1," SIP:99990036@192.30.32.68" ;," **********"" "" 99990036",1,31,101," 0.0.0.0",0&安培; 297,1," SIP:99990037@192.30.32.68" ;," **********"" "" 99990037",1,31,101," 0.0.0.0",0&安培; 298,3," SIP:99990039@192.30.32.68" ;," **********"" #&34;," 99990039",1,31,101," 0.0.0.0",0 ACK:2018-03-08 12:12:28:4802:RETN = 0,ID = 1,NUM = 2,RESULT = 299,3," sip:99990038@192.30.32.68",& #34; **********"" "" 99990038",1,31,101," 0.0.0.0",0&安培; 700,1," SIP:99990040@192.30.32.68" ;," **********"" "" 99990040",1,31,101," 0.0.0.0",0
找到每个子字符串,如:
290,1," SIP:99990030@192.30.32.68"," **********"" "" 99990030",1,31,106," 0.0.0.0",0
我试过
Pattern pattern1 = Pattern.compile("(\\d+),(\\d+),\"sip:(.+)\",.+,.+,\"(\\d+)\",.+,.+,.+,.+,.+?&");
但这不起作用。
编辑: 根据回应我这样做了;
Pattern pattern =Pattern.compile("(\\d+),(\\d+),\"sip:(.+)\",\".*\",\".*\",\"(\\d+)\",\\d+,\\d+,\\d+,\".*\",\\d+");
Matcher matcher;
String teststr="................";
String[] test2=teststr.split("&");
List<String> result=new ArrayList<>();
for(String s:test2){
matcher=pattern.matcher(s);
if(matcher.find()) result.add(matcher.group(0));
}
result.forEach(System.out::println);
编辑2: 弗朗西斯科的回应也很好。 谢谢大家。
答案 0 :(得分:1)
在JShell中进行测试很难看,但在Scala中,我们使用&#34;&#34;&#34;&#34;进行字符串屏蔽。一些多线串&#34;其中&#34;包含引号&#34;&#34;&#34;,所以我在那里做了:
val log = """ACK : 2018-03-08 12:12:28 : 4802 : RETN=0,ID=1,NUM=9,RESULT=290,1,"sip:99990030@192.30.32.68","**********"," ","99990030",1,31,106,"0.0.0.0",0&291,1,"sip:99990031@192.30.32.68","**********"," ","99990031",1,31,101,"0.0.0.0",0&292,1,"sip:99990032@192.30.32.68","**********"," ","99990032",1,31,101,"0.0.0.0",0&293,1,"sip:99990033@192.30.32.68","**********"," ","99990033",1,31,101,"0.0.0.0",0&294,1,"sip:99990034@192.30.32.68","**********"," ","99990034",1,31,101,"0.0.0.0",0&295,1,"sip:99990035@192.30.32.68","**********"," ","99990035",1,31,101,"0.0.0.0",0&296,1,"sip:99990036@192.30.32.68","**********"," ","99990036",1,31,101,"0.0.0.0",0&297,1,"sip:99990037@192.30.32.68","**********"," ","99990037",1,31,101,"0.0.0.0",0&298,3,"sip:99990039@192.30.32.68","**********"," ","99990039",1,31,101,"0.0.0.0",0 ACK : 2018-03-08 12:12:28 : 4802 : RETN=0,ID=1,NUM=2,RESULT=299,3,"sip:99990038@192.30.32.68","**********"," ","99990038",1,31,101,"0.0.0.0",0&700,1,"sip:99990040@192.30.32.68","**********"," ","99990040",1,31,101,"0.0.0.0",0"""
val parts = log.split ("&")
val lines = parts.map (s=> s.replaceAll (".*RESULT=", "")
lines.foreach (println)
结果:
290,1,"sip:99990030@192.30.32.68","**********"," ","99990030",1,31,106,"0.0.0.0",0
291,1,"sip:99990031@192.30.32.68","**********"," ","99990031",1,31,101,"0.0.0.0",0
292,1,"sip:99990032@192.30.32.68","**********"," ","99990032",1,31,101,"0.0.0.0",0
293,1,"sip:99990033@192.30.32.68","**********"," ","99990033",1,31,101,"0.0.0.0",0
294,1,"sip:99990034@192.30.32.68","**********"," ","99990034",1,31,101,"0.0.0.0",0
295,1,"sip:99990035@192.30.32.68","**********"," ","99990035",1,31,101,"0.0.0.0",0
296,1,"sip:99990036@192.30.32.68","**********"," ","99990036",1,31,101,"0.0.0.0",0
297,1,"sip:99990037@192.30.32.68","**********"," ","99990037",1,31,101,"0.0.0.0",0
299,3,"sip:99990038@192.30.32.68","**********"," ","99990038",1,31,101,"0.0.0.0",0
700,1,"sip:99990040@192.30.32.68","**********"," ","99990040",1,31,101,"0.0.0.0",0
这遵循&#34;使用可能有效的最简单的事情&#34;
答案 1 :(得分:0)
我不认为你太过分了:
\d{3},\d,\"sip:\d{8}@\d{3}\.\d{1,3}\.\d{1,3}\.\d{1,3}","\*{10}","[" ]","\d{8}",\d,\d{2},\d{3},"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}",\d
现场演示here。
当然,要将其合并到Jave中,您可以使用
Pattern pattern1 = Pattern.compile(regularExpression, Pattern.LITERAL);
所以你不必逃避所有的反斜杠。