[英]Extract string between the tags in Java
I have string like below 我有下面的字符串
Msg_Begin
Some message1
Msg_End
Msg_Begin
Some message2
Msg_End
Msg_Begin
Some message3
Msg_End
And want to get the message between Msg_Begin and Msg_End in to the list like 并想要获取Msg_Begin和Msg_End之间的消息到列表中,例如
[Some message1, Some message2, Some message3]
what is the best approach for this in Java. 用Java最好的方法是什么?
String messages = originalString.replaceAll("Msg_Begin","");
String[] array = messages.split("Msg_End");
return Arrays.asList(array);
Just make sure that your messages do not contain Msg_Begin
or Msg_End
. 只要确保您的消息不包含
Msg_Begin
或Msg_End
。
You can achieve that with a regular expression : 您可以使用正则表达式来实现:
//Filling Your test case and print
String entry = "Msg_Begin\r\n" +
"Some message1\r\n" +
"Msg_End\r\n" +
"Msg_Begin\r\n" +
"Some message2\r\n" +
"Msg_End\r\n" +
"Msg_Begin\r\n" +
"Some message3\r\n" +
"Msg_End";
System.out.println("IN : \r\n" + entry) ;
//Compile the regular expression patern, providing the DOTALL flag to enable mutiline matches
Pattern p = Pattern.compile("Msg_Begin\r\n(.+?)\r\nMsg_End(\r\n)?", Pattern.DOTALL) ;
Matcher m = p.matcher(entry) ;
// iterate over results (for exemple add them to a list)
System.out.println("\r\nOUT :") ;
List<String> list = new ArrayList<>();
while (m.find()) {
list.add( m.group(1));
System.out.println(m.group(1)) ;
}
Produce the following result : 产生以下结果:
IN :
Msg_Begin
Some message1
Msg_End
Msg_Begin
Some message2
Msg_End
Msg_Begin
Some message3
Msg_End
OUT :
Some message1
Some message2
Some message3
More informations abour regular expressions syntax can be found here 正则表达式语法的更多信息可以在这里找到
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.